Arrays
Array: an ordered set of elements referenced by index.
Javascript supports both ordinary and associative arrays.
It has an Array object:
- Create an Array with the new method, e.g.:
var items = new Array()
or
var items = new Array(20)
- Assign an element, e.g.:
items[i] = value
- Apply various methods, including joining, reversing and sorting.
Our example has an array with 12 elements.
In the source code, observe:
Lab
Extra for Experts
- Copy the example from the top of the chapter and edit it as follows.
- Add a third field to elicit the year.
- Add logic to handle leap years.
- You will need the modulo (%) arithmetic operator
- Allow the user to
enter the data into the fields in any order.
- Add a button for the user to click to check that the number
of days is correct for that month in that year.
- Of course, only February's number of days varies from year to year.
- Solution: