Arrays, date, random 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 =) 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 month Random Selection • Choose between options randomly var n = Math.random(); • [Math is a collection of functions] • Need to save it to reuse! • Returns value between 0 and 1 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); Conditional Examples • Random message using if • Simple if…then…else • Condition is random function 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.asp 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, … 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)