JavaScript Arrays Date
Arrays
Producing different results We have looked at how to compute different numeric values Computation Random How do we create different strings? Can do it from user input But want to translate a number to a string Examples: Random facts Convert month or day number to string
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: Date returns number, you return that month
Create your first array Change a number to text Create a JavaScript function called spellIt Takes in a number from 0 to 9 Returns a string with the number name Call it with a button press, passing the user input
Date
Date() Returns a collection of information Year, month, day, hours, minutes, seconds, milliseconds
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 day return? Number 0 to 6 Want to convert to Sunday, Monday, … var DayOfWeek = [“Sun”, “Mon”, “Tues”, “Wed”, “Thurs”, “Fri”, “Sat”];
Display the current month Create a JavaScript function called monthName To be in the same file as spellIt No parameters needed Returns a string with this month’s name
Extend spellIt Accept a number 20-29 and spell it out Subtract 20 twenty, twenty-one, twenty-two, …, twenty-nine Subtract 20 Will need to change the array to include - What should the 0 array value be?
HTML Page It should print the date of your last major assignment User enters a number between 20 and 29 Button press returns the month and date spelled out Example: Enter 27 Returns April twenty-seven