Download presentation
Presentation is loading. Please wait.
1
10 November JavaScript
2
Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk, don’t read Pictures when possible If you ask a question, wait 30 seconds
3
Defining a Variable Statement used is called a declaration Format: var name = value; name is the identifier value is the initial value given to the variable Variable Names The language is case sensistive yourname, Yourname, YourName are three different variables Recommendation: always use the same style Initial values You do not need to initialize variables Do need to give them a value before you try to do anything useful with them
4
Examples var TaxRate =.087; TaxRate is a variable that contains a numeric value representing the tax rate to be used and has an initial value of.087 var FirstName = “”; FirstName is a variable that contains a character string representing a person’s first name and has an initial value of nothing var done; done is variable that will track the state of ongoing work and has no initial value
5
Types of Data 3 types: Numeric Does not distinguish between integers and real numbers Can use either standard or scientific notation Do not use commas String Can be enclosed in either single (‘) or double (“) quotes If need to use one in the string, use the other for the whole string “” is called the empty string or the null string Boolean Two values: true, false Reserved words. Do not use as variable names
6
Multiple declarations Can use a separate statement for every variable or declare a list of variables var FirstName = “”, LastName = “”; var FirstName = “”; var LastName = “”; Statements are equivalent Can have some initialized and some not
7
Documenting Your Code Documenting your code is not necessary for the compiler or interpreter, but is typically needed by people – including yourself! ALWAYS document what you write Information for people only are comments
8
Comments Two Forms /* … */ // /* … */ Everything between the delimiters is a comment and not processed Can cross multiple lines Can start anywhere // Everything from there until the end of the line is not processed
9
var start = 0; // beginning of the string var start = 0; /* identifies the place to start processing */ var start = 0; /* beginning of the string var end = 0; Comment Examples WRONG RIGHT
10
What to do with Variables Simplest action is to assign a value: assignment statement Name = “Tom”; Price = 13.25; Can use any type of expression on the right – both constants and variables
11
Expressions: Arithmetic There are rules of precedence 1+b*c Do I multiply or add first? If you use parentheses, you don’t have to remember! Binary operations +, -, /, *, % (modulus or remainder) Unary operations Negation
12
Expressions: String Primary one is concatenation Uses “+” If either operand is a string, means concatenate Else means add
13
Expressions: Boolean Comparisons Numeric or string >, <, == Not and combinations Logical operations And, or, not
14
Logical Tables 0 = false, 1 = true And 0 && 0 = 0 0 && 1 = 0 1 && 0 = 0 1 && 1 = 1 Or 0 || 0 = 0 0 || 1 = 1 1 || 0 = 1 1 || 1 = 1 Not !0 = 1 !1 = 0
15
Practice Write a statement that computes total price including tax and shipping computes the area of a circle calculates the average speed of a car trip
16
Using statements in forms An example: Shots: <input name="shots“ onclick="num_shots= 1" type="radio"> 1 <input name="shots" onclick="num_shots = 2" type="radio"> 2
17
How to Change What You Do Conditional statements Iterations (doing it more than once) Functions (an algorithm that can be executed more than once) All need compound statements – multiple statements that can be processed as one { stmt; stmt; stmt; }
18
But first, JavaScript in Your Browser Simple JavaScript in body … code … Simplest statement document.write(“text string")
19
Separating Functions & Forms We’ll separate forms and functions Separate out all the actions that we will do Put them in the header Simpler forms In the … variable definitions … … functions ….
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.