Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: forEach Lines
How many lines are printed?
["a", "b", "c"].forEach((x) => console.log(x));3102Hint : 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);undefined[2, 4]2,42Hint : 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));0 then 11 then 2Zap then MetachuundefinedHint : 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);[10, 20, 30][1, 2, 3]60undefinedHint : 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);42true0Hint : 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]);undefined21Hint : 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);[8, 9][3, 1][false, true, false, true]2Hint : 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);0undefined3Hint : 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);21365Hint : 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?
filtermapforEachHint : 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));9[9, 12]122Hint : The first match only.
Exercise 15 β Quiz: find Nothing
What does this code print?
console.log([1, 2, 3].find((n) => n > 5));undefined[]-1falseHint : 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);151undefinedMetachuHint : 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));180trueHint : A position, starting at 0.
Exercise 19 β Quiz: findIndex Nothing
What does this code print?
console.log([1, 2].findIndex((n) => n > 5));-1undefined02Hint : 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);[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);[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);3620Hint : 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?
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?
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.