Every item has a position
Show on a big screenMonday's reading is one item in the list. A program cannot say "the Monday one". It must say a number.
Loading lab…
- Five boxes: 6, 46, 2, 8, 0. Positions run 0 to 4.
week[0]reads position 0 → 6.week[2]reads position 2 → 2.week[4]reads the last position → 0.week[5]asks past the end. In this editor, the run stops.
The rule
A position is an item's place in the list, counted from 0. Past the end means after the last item. A blank value means nothing is there. A run is one time the program is started.
Every item has a position, starting at 0. The last one sits at .length - 1. In this editor, asking past the end stops the run. The message names both numbers. Other languages may hand back a blank value instead.
Five days are at positions 0 to 4. The count is 5. The last position is 5 − 1 = 4, one less than the count.
Position 5 does not exist in a 5-item list. In this editor, the run stops with this sentence:
You asked for position 5, but the list has 5 items.
Try the lab to finish this lesson.