Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Empty Array
Which line creates an empty array?
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]);"Zap""Metachu"undefined0Hint : 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]);undefined"Charge"nullHint : 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);213"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]);159undefined2Hint : Compute the index between the brackets first.
Exercise 6 β Quiz: Reach the End
Print the last item of the bag, whatever its 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]);"Zap""Pyrochu""Aquachu"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);432Hint : 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);321"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]);"Sam""Lea""Noa"3Hint : 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.
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);"Elixir""Potion"1undefinedHint : 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]);"Lea""Sam""Noa"undefinedHint : 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());undefined0nullHint : 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.
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"));falsetrue0-1Hint : 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"));23true1Hint : 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"));-1undefinedfalse0Hint : 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.
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]);
}undefined50302Hint : 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?
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.
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);20103"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);["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"));2"Thunder"["Spark", "Thunder"]undefinedHint : The method returns information about the array after the addition.