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.

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.

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.

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.

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.

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