Variables and Data Types JavaScript Variables and Data Types
Holding information
Holding information Computer doesn’t understand values It knows about locations: “cubby holes” Takes locations and does what you tell it: read, write, add
A = B + C; example Cubby Hole 2: add this value Cubby Hole 3: place it here A = B + C; Cubby Hole 1: take this value
Defining CUBBY HOLES They are called variables Define them Give them values Take values from them
= means “takes the value of” A = B + C; A takes the value of B+C
Changing Information A = A + C; Cubby Hole 2: add this value Cubby Hole 1: replace the old value A = A + C; Cubby Hole 1: take this value
To DEFINE var anyName; Defines a variable called anyName Case-sensitive Can be any type of value var aNumber = 1; aNumber is a number Starts with a value of 1
VALUES
Random Numbers
Why random numbers? Variability Different results Examples Display one of ten pictures Display one of five quotations Play a game of chance All built on a random number!
Can computers really create random numbers? Computers do as they are told So how can they do something random? They can’t! But we can tell them to find a number that changes often! Needs to not have an easily recognized pattern Pseudo-random Number Generator Options? Time!
Getting a random number Math.random() Returns a value between 0 and 1 One of a series of mathematical functions members of a collection called Math each are prefixed by “Math.”
What if you don’t want 0-1? Change range? Multiply Not start at 0? Add Integers only? Round Floor Ceiling
Using JavaScript
Class Plan Today: jsfiddle Thursday After break Output Console.log for helping ourselves Alerts for “final” results Thursday Create “functions” that can be called from the onclick Output still alerts After break Ability to read from forms and write to the page