Javascript · Arrays (Basics)
Accessing Elements
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Picking One Element
An array keeps its elements in order, and each one has a position called an index. To read an element, write the array name followed by the index in square brackets:
const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team[0]); console.log(team[1]); console.log(team[2]);
Surprise: the first index is `0`, not `1`! Aquachu is at index 0, Pyrochu at index 1, Florachu at index 2. This is the most common source of confusion with arrays, so remember: counting starts at 0.
Exercise 2 — Out of Bounds
What if you ask for an index that doesn't exist? JavaScript doesn't crash: it simply gives you `undefined`.
const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team[3]); console.log(team[10]);
With 3 elements, the valid indexes are 0, 1 and 2. Index 3 is already one too far. Keep an eye on this: an `undefined` showing up in your output usually means an index is off by one.
Negative indexes
`team[-1]` doesn't give the last element in JavaScript, unlike some other languages. It's just another index that doesn't exist, so you get `undefined`.
Exercise 3 — Quiz: Which One?
What does this code print?
const moves = ["Splash", "Bubble", "Wave", "Tide"]; console.log(moves[1]);
Options
• ```"Bubble"```
• ```"Splash"```
• ```"Wave"```
• ```undefined```
Hint : Count the positions starting from the very first value an index can take.
Exercise 4 — Reach Florachu
Complete the index so the code prints `Florachu`.
const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team[____]);
Options: 2 3 1 0
Hint : Florachu is the third one, but the third position doesn't have the number 3.
Exercise 5 — An Index Is Just a Number
The value you read from an array can be stored in a variable or used in any expression, like any other value:
const team = ["Aquachu", "Pyrochu", "Florachu"]; let leader = team[0]; console.log(leader + " leads the team!");
And the index itself doesn't have to be written by hand: it can be a variable, or even a calculation. Anything that gives a number works.
const team = ["Aquachu", "Pyrochu", "Florachu"]; let chosen = 2; console.log(team[chosen]); console.log(team[chosen - 1]);
Exercise 6 — Quiz: One Too Far
What does this code print?
const levels = [5, 9, 12]; console.log(levels[3]);
Options
• ```undefined```
• ```12```
• ```3```
• An error
Hint : How many elements are there, and what is the highest valid index?
Exercise 7 — Variable Index
Use `bestIndex` to print the name of the best Codémon.
const names = ["Rockachu", "Zap", "Metachu"]; let bestIndex = 1; console.log("Best: " + names____bestIndex____);
Options: [ ] ( ) .
Hint : Reading an element always uses the same brackets, whether the index is a number or a variable.
Exercise 8 — Index Match
Match each expression with its value.
const team = ["Aquachu", "Pyrochu", "Florachu"];
Items to match
• team[0]
• team[2]
• team[3]
• team[1]
Definitions
• "Aquachu"
• "Florachu"
• undefined
• "Pyrochu"
Exercise 9 — Calculating with Elements
The elements of a number array are ordinary numbers: add them, compare them, use them in conditions.
const hp = [45, 30, 60]; let total = hp[0] + hp[1] + hp[2]; console.log("Total HP: " + total);
Same with comparisons. Here we check which of the first two Codémon is stronger:
const levels = [7, 12, 9]; if (levels[0] > levels[1]) { console.log("The first one is stronger"); } else { console.log("The second one is stronger"); }
Exercise 10 — Quiz: Adding Up
What does this code print?
const hp = [40, 25, 35]; console.log(hp[0] + hp[2]);
Options
• ```75```
• ```65```
• ```60```
• ```"4035"```
Hint : Locate each element by its index first, then do the math.
Exercise 11 — Team Report
`team` and `levels` go together: the level at index 0 belongs to the Codémon at index 0, and so on. 1. Print `"Leader: X (level Y)"` using the first Codémon and its level 2. Print `"Rookie: X (level Y)"` using the last Codémon and its level 3. Print `"Total level: Z"` with the sum of the three levels
Starter code
const team = ["Aquachu", "Pyrochu", "Florachu"]; const levels = [12, 8, 5]; // Write your code here
Back to the course