Download presentation
Presentation is loading. Please wait.
Published byRosamund Floyd Modified over 9 years ago
1
Arrays, date, random
2
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 =)
3
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
4
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
5
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);
6
Conditional Examples Random message using if Simple if…then…else Condition is random function
7
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
8
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, …
9
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.