10 November JavaScript
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
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
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
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
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
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
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
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
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
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
Expressions: String Primary one is concatenation Uses “+” If either operand is a string, means concatenate Else means add
Expressions: Boolean Comparisons Numeric or string >, <, == Not and combinations Logical operations And, or, not
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
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
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
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; }
But first, JavaScript in Your Browser Simple JavaScript in body … code … Simplest statement document.write(“text string")
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 ….