Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” Quiz: forEach Lines

How many lines are printed?

["a", "b", "c"].forEach((x) => console.log(x));
Options
β€’ 3
β€’ 1
β€’ 0
β€’ 2

Hint : One call per element.

Exercise 2 β€” Quiz: forEach Returns

What does this code print?

const r = [1, 2].forEach((n) => n * 2); console.log(r);
Options
β€’ undefined
β€’ [2, 4]
β€’ 2,4
β€’ 2

Hint : This method is for doing, not for producing.

Exercise 3 β€” Quiz: Index Parameter

What does this code print?

["Zap", "Metachu"].forEach((name, i) => console.log(i));
Options
β€’ 0 then 1
β€’ 1 then 2
β€’ Zap then Metachu
β€’ undefined

Hint : The second parameter is the position.

Exercise 4 β€” Quiz: Print Each

Print every move.

Hint : The method that acts on each element, and the parameter used in the body.

Exercise 5 β€” Quiz: map Result

What does `r` contain?

const r = [1, 2, 3].map((n) => n * 10);
Options
β€’ [10, 20, 30]
β€’ [1, 2, 3]
β€’ 60
β€’ undefined

Hint : Each element is transformed by the callback.

Exercise 6 β€” Quiz: map Length

What does this code print?

const r = [5, 10, 15, 20].map((n) => n > 10); console.log(r.length);
Options
β€’ 4
β€’ 2
β€’ true
β€’ 0

Hint : map never changes the number of elements.

Exercise 7 β€” Quiz: map Without return

What does this code print?

const r = [1, 2].map((n) => { n + 1; }); console.log(r[0]);
Options
β€’ undefined
β€’ 2
β€’ 1
β€’ An error

Hint : Braces without return produce nothing.

Exercise 8 β€” Quiz: Names Only

Build an array of the names.

Hint : A new array built from one property.

Exercise 9 β€” Quiz: filter Result

What does `r` contain?

const r = [3, 8, 1, 9].filter((n) => n > 5);
Options
β€’ [8, 9]
β€’ [3, 1]
β€’ [false, true, false, true]
β€’ 2

Hint : Only elements passing the test stay.

Exercise 10 β€” Quiz: Empty filter

What does this code print?

const r = [1, 2, 3].filter((n) => n > 10); console.log(r.length);
Options
β€’ 0
β€’ undefined
β€’ 3
β€’ An error

Hint : No match gives an empty array, not an error.

Exercise 11 β€” Quiz: filter Objects

What does this code print?

const team = [ { name: "Aquachu", hp: 40 }, { name: "Pyrochu", hp: 0 }, { name: "Zap", hp: 25 } ]; console.log(team.filter((c) => c.hp > 0).length);
Options
β€’ 2
β€’ 1
β€’ 3
β€’ 65

Hint : Count the objects that pass the test.

Exercise 12 β€” Quiz: Keep the Big Ones

Keep the values of 50 or more.

Hint : "50 or more" includes 50.

Exercise 13 β€” Quiz: map vs filter

Which method changes the number of elements?

Options
β€’ filter
β€’ map
β€’ forEach
β€’ Both map and filter

Hint : One keeps a subset, the other keeps them all.

Exercise 14 β€” Quiz: find Result

What does this code print?

console.log([2, 7, 9, 12].find((n) => n > 8));
Options
β€’ 9
β€’ [9, 12]
β€’ 12
β€’ 2

Hint : The first match only.

Exercise 15 β€” Quiz: find Nothing

What does this code print?

console.log([1, 2, 3].find((n) => n > 5));
Options
β€’ undefined
β€’ []
β€’ -1
β€’ false

Hint : find and filter answer differently when nothing matches.

Exercise 16 β€” Quiz: find an Object

What does this code print?

const team = [ { name: "Zap", level: 4 }, { name: "Metachu", level: 15 } ]; const m = team.find((c) => c.name === "Metachu"); console.log(m.level);
Options
β€’ 15
β€’ 1
β€’ undefined
β€’ Metachu

Hint : The whole object comes back, with all its properties.

Exercise 17 β€” Quiz: Find Zap

Find Zap and print its HP.

Hint : One object, then one of its properties.

Exercise 18 β€” Quiz: findIndex Result

What does this code print?

console.log([4, 8, 15].findIndex((n) => n > 5));
Options
β€’ 1
β€’ 8
β€’ 0
β€’ true

Hint : A position, starting at 0.

Exercise 19 β€” Quiz: findIndex Nothing

What does this code print?

console.log([1, 2].findIndex((n) => n > 5));
Options
β€’ -1
β€’ undefined
β€’ 0
β€’ 2

Hint : Same answer as indexOf for a missing value.

Exercise 20 β€” Quiz: Chain Result

What does `r` contain?

const r = [1, 2, 3, 4] .filter((n) => n > 2) .map((n) => n * 2);
Options
β€’ [6, 8]
β€’ [2, 4, 6, 8]
β€’ [3, 4]
β€’ [6]

Hint : First step on the whole array, second step on the result.

Exercise 21 β€” Quiz: Chain Order

What does `r` contain?

const r = [1, 2, 3, 4] .map((n) => n * 2) .filter((n) => n > 2);
Options
β€’ [4, 6, 8]
β€’ [6, 8]
β€’ [2, 4, 6, 8]
β€’ [3, 4]

Hint : Double first, then filter the doubled values.

Exercise 22 β€” Quiz: Count in a Chain

Count the levels above 5 in a single chain.

Hint : Select, then measure.

Exercise 23 β€” Quiz: Original Untouched

What does this code print?

const a = [1, 2, 3]; const b = a.map((n) => n * 2); const c = a.filter((n) => n > 1); console.log(a.length);
Options
β€’ 3
β€’ 6
β€’ 2
β€’ 0

Hint : Neither method modifies the array it's called on.

Exercise 24 β€” Quiz: End of Chain

Which of these can NOT be followed by another array method?

Options
β€’ arr.forEach(f)
β€’ arr.map(f)
β€’ arr.filter(f)
β€’ arr.filter(f).map(g)

Hint : One of them returns nothing at all.

Exercise 25 β€” Quiz: Which Method?

You want the one CodΓ©mon named Zap, as an object. Which method?

Options
β€’ team.find((c) => c.name === "Zap")
β€’ team.filter((c) => c.name === "Zap")
β€’ team.includes("Zap")
β€’ team.map((c) => c.name)

Hint : One method returns a single element, another returns an array.

Back to the course