Javascript · Arrays (Basics)
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Quiz: Empty Array
Which line creates an empty array?
Options
• ```let bag = [];```
• ```let bag = {};```
• ```let bag = ();```
• ```let bag = "";```
Hint : Only one pair of brackets means "array" in JavaScript.
Exercise 2 — Quiz: First Element
What does this code print?
const team = ["Zap", "Metachu", "Rockachu"]; console.log(team[0]);
Options
• ```"Zap"```
• ```"Metachu"```
• ```undefined```
• ```0```
Hint : Where does the numbering of positions start?
Exercise 3 — Quiz: Missing Index
What does this code print?
const moves = ["Spark", "Thunder", "Charge"]; console.log(moves[3]);
Options
• ```undefined```
• ```"Charge"```
• An error
• ```null```
Hint : List the valid indexes of a 3-element array.
Exercise 4 — Quiz: Size
What does this code print?
const bag = ["Potion", "Map"]; console.log(bag.length);
Options
• ```2```
• ```1```
• ```3```
• ```"Map"```
Hint : This property counts the elements.
Exercise 5 — Quiz: Last Value
What does this code print?
const levels = [4, 9, 15]; console.log(levels[levels.length - 1]);
Options
• ```15```
• ```9```
• ```undefined```
• ```2```
Hint : Compute the index between the brackets first.
Exercise 6 — Quiz: Reach the End
Print the last item of the bag, whatever its size.
const bag = ["Potion", "Map", "Elixir"]; console.log(bag[bag.____ ____ 1]);
Options: length - + size
Hint : The number of elements and the last index are not the same number.
Exercise 7 — Quiz: Replaced
What does this code print?
const team = ["Aquachu", "Pyrochu", "Florachu"]; team[1] = "Zap"; console.log(team[1]);
Options
• ```"Zap"```
• ```"Pyrochu"```
• ```"Aquachu"```
• An error
Hint : Assigning to an index replaces what was there.
Exercise 8 — Quiz: Stretched
What does this code print?
const scores = [10, 20]; scores[3] = 40; console.log(scores.length);
Options
• ```4```
• ```3```
• ```2```
• An error
Hint : The length is always one more than the highest index in use.
Exercise 9 — Quiz: After push
What does this code print?
const moves = ["Spark"]; moves.push("Thunder", "Charge"); console.log(moves.length);
Options
• ```3```
• ```2```
• ```1```
• ```"Charge"```
Hint : This method can add more than one element at a time.
Exercise 10 — Quiz: Front of the Line
What does this code print?
const line = ["Lea", "Noa"]; line.unshift("Sam"); console.log(line[0]);
Options
• ```"Sam"```
• ```"Lea"```
• ```"Noa"```
• ```3```
Hint : Which end of the array does this method add to?
Exercise 11 — Quiz: Both Ends
Add `Potion` at the end of the bag and `Sam` at the front of the line.
const bag = ["Map"]; const line = ["Lea"]; bag.____("Potion"); line.____("Sam"); console.log(bag); console.log(line);
Options: push unshift pop shift
Hint : Two methods add elements; each one works on a different end.
Exercise 12 — Quiz: What pop Gives
What does this code print?
const bag = ["Potion", "Elixir"]; let item = bag.pop(); console.log(item);
Options
• ```"Elixir"```
• ```"Potion"```
• ```1```
• ```undefined```
Hint : This method removes from one specific end and returns what it removed.
Exercise 13 — Quiz: After shift
What does this code print?
const line = ["Sam", "Lea", "Noa"]; line.shift(); console.log(line[0]);
Options
• ```"Lea"```
• ```"Sam"```
• ```"Noa"```
• ```undefined```
Hint : Once the first element is gone, the others move up.
Exercise 14 — Quiz: Empty pop
What does this code print?
const bag = []; console.log(bag.pop());
Options
• ```undefined```
• An error
• ```0```
• ```null```
Hint : There is nothing to remove, and JavaScript has a value for "nothing".
Exercise 15 — Quiz: Serve Everyone
Serve every trainer in order of arrival until the line is empty.
const line = ["Sam", "Lea", "Noa"]; while (line.length ____ 0) { let trainer = line.____(); console.log("Serving " + trainer); }
Options: > < shift pop ===
Hint : The loop runs while something remains, and the order of arrival decides which end to take from.
Exercise 16 — Quiz: Case Matters
What does this code print?
const team = ["Zap", "Metachu"]; console.log(team.includes("zap"));
Options
• ```false```
• ```true```
• ```0```
• ```-1```
Hint : Compare the two spellings character by character.
Exercise 17 — Quiz: Found Where?
What does this code print?
const moves = ["Spark", "Thunder", "Charge"]; console.log(moves.indexOf("Charge"));
Options
• ```2```
• ```3```
• ```true```
• ```1```
Hint : The result is an index, and indexes start where they always do.
Exercise 18 — Quiz: Not Found
What does this code print?
const team = ["Aquachu", "Pyrochu"]; console.log(team.indexOf("Zap"));
Options
• ```-1```
• ```undefined```
• ```false```
• ```0```
Hint : This method has a special value for "not here", and it is not a boolean.
Exercise 19 — Quiz: Is Zap Here?
Print `Zap is ready!` only if Zap is in the team.
const team = ["Aquachu", "Zap"]; if (team.____("Zap")) { console.log("Zap is ready!"); }
Options: includes indexOf length has
Hint : The condition needs a true or false answer.
Exercise 20 — Quiz: One Turn Too Many
What is printed on the last line?
const hp = [30, 50]; for (let i = 0; i <= hp.length; i++) { console.log(hp[i]); }
Options
• ```undefined```
• ```50```
• ```30```
• ```2```
Hint : Compare the last value of the counter with the last valid index.
Exercise 21 — Quiz: for...of Variable
In `for (let cdm of team)`, what does `cdm` hold at each turn?
Options
• An element of the array
• The index of the element
• The length of the array
• The whole array
Hint : This loop was designed so you don't have to deal with positions.
Exercise 22 — Quiz: Greet Them All
Print a greeting for each Codémon of the team.
const team = ["Aquachu", "Pyrochu"]; for (let cdm ____ team) { console.log("Hello " + ____); }
Options: of in cdm team i
Hint : The keyword is specific to this loop, and the greeting must use the loop variable.
Exercise 23 — Quiz: Sum It Up
What does this code print?
const levels = [4, 6, 10]; let total = 0; for (let level of levels) { total += level; } console.log(total);
Options
• ```20```
• ```10```
• ```3```
• ```"4610"```
Hint : Add the elements one by one, in order.
Exercise 24 — Quiz: const Array
What happens when this code runs?
const team = ["Aquachu"]; team = ["Zap"]; console.log(team);
Options
• An error: reassigning a const
• ```["Zap"]```
• ```["Aquachu"]```
• ```["Aquachu", "Zap"]```
Hint : Look at what the second line does to the variable itself, not to its content.
Exercise 25 — Quiz: push Returns
What does this code print?
const moves = ["Spark"]; console.log(moves.push("Thunder"));
Options
• ```2```
• ```"Thunder"```
• ```["Spark", "Thunder"]```
• ```undefined```
Hint : The method returns information about the array after the addition.
Back to the course