9
Arrays, date, random

Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

Embed Size (px)

Citation preview

Page 1: Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

Arrays, date, random

Page 2: Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

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 =)

Page 3: Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

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

Page 4: Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

Random Selection

• Choose between options randomlyvar n = Math.random();

• [Math is a collection of functions]

• Need to save it to reuse!

• Returns value between 0 and 1

Page 5: Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

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);

Page 6: Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

Conditional Examples

• Random message using if

• Simple if…then…else

• Condition is random function

Page 7: Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

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

Page 8: Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

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, …

Page 9: Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is

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)