Explicitly define them Can give them starting values Figures out type Case sensitive var x 5 note this is the ONLY use of Arrays Collection of related information Referenced with index ID: 757802
Download Presentation The PPT/PDF document "Arrays, date, random Declared Variables" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Arrays, date, randomSlide2
Declared Variables
Explicitly
define them
Can give them starting values
Figures out type
Case sensitive
var
x = 5;
(note: this is the ONLY use of =)Slide3
Arrays
Collection of related
information
Referenced with index
Easy way to choose between items
var
array
= [
“A",
"
B", “F",
"
G"
];
Just keep adding items!
array[index]
Start with 0
Example: user enters number, you return that monthSlide4
Random Selection
Choose between options randomly
v
ar
n =
Math.random
();
[Math is a collection of functions]
Need to save it to reuse!
Returns value between 0 and 1Slide5
Converting Random to Integer
Often useful to convert that random number to an integer
Index into array!
0->1 needs to be changed to 0->3 (or any other number)
var
biggerNumber
= n*4;
gets the range correct
But only want integer:
Math.floor
returns the largest integer less than the value
var
biggerInteger
=
Math.floor
(n*4);Slide6
Conditional Examples
Random message using if
Simple if…then…else
Condition is random functionSlide7
Date and Time
Full date is unfriendly format
To get today’s date:
var
d = new Date();
To get the time:
var
time =
d.getHours
();
To get the day:
var
theDay
=
d.getDay
();
w3schools
http://www.w3schools.com/js/js_date_methods.aspSlide8
What does it mean?
Objects have attributes
Chair: color, size, legs, wheels, …
Date: year, month, day, day of week, hour, …
Object.retrieve
-attribute
Assign the date with built-in function Date()
Extract the piece using
getHours
,
getDays
, …Slide9
Date and Time Examples
3 messages based on Hour
Prints an appropriate message based on time of day (3 choices)
2 messages based on Day
Prints an appropriate message based on day of week (2 choices)