Download presentation
Presentation is loading. Please wait.
1
Variables and Data Types
JavaScript Variables and Data Types
2
Holding information
3
Holding information Computer doesn’t understand values
It knows about locations: “cubby holes” Takes locations and does what you tell it: read, write, add
4
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
5
Defining CUBBY HOLES They are called variables Define them
Give them values Take values from them
6
= means “takes the value of” A = B + C; A takes the value of B+C
7
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
8
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
9
VALUES
10
Random Numbers
11
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!
12
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!
13
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.”
14
What if you don’t want 0-1? Change range? Multiply Not start at 0? Add
Integers only? Round Floor Ceiling
15
Using JavaScript
16
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.