Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: At Least One
What does this code print?
console.log([4, 8, 12].some((l) => l >= 10));truefalse121Hint : One element passing is enough.
Exercise 2 β Quiz: All of Them
What does this code print?
console.log([4, 8, 12].every((l) => l >= 10));falsetrue2[12]Hint : A single failure settles it.
Exercise 3 β Quiz: Empty every
What does this code print?
console.log([].every((x) => x > 0));truefalseundefinedHint : No element fails the test here.
Exercise 4 β Quiz: Anyone Standing?
Print whether at least one member still has HP.
Hint : "At least one" has its own method, and having HP means above zero.
Exercise 5 β Quiz: The Sum
What does this code print?
console.log([1, 2, 3].reduce((a, b) => a + b, 0));603123Hint : The accumulator is carried from one element to the next.
Exercise 6 β Quiz: Starting Value
What does this code print?
console.log([1, 2].reduce((a, b) => a + b, 100));1033100102Hint : The second argument is where the accumulator begins.
Exercise 7 β Quiz: reduce on Objects
What does this code print?
const team = [
{ level: 4 },
{ level: 8 }
];
console.log(team.reduce((s, c) => s + c.level, 0));122048Hint : Read the property of each object before adding.
Exercise 8 β Quiz: Total the HP
Add up the HP of the team.
Hint : The method that boils a list down, and the neutral value of a sum.
Exercise 9 β Quiz: The Slice
What does this code print?
console.log([10, 20, 30, 40].slice(1, 3));20,3020,30,4010,2030Hint : The first index joins, the second marks where to stop.
Exercise 10 β Quiz: Original After slice
What does this code print?
const a = [1, 2, 3];
a.slice(0, 1);
console.log(a.length);3120Hint : This method belongs to the family that returns something new.
Exercise 11 β Quiz: After splice
What does this code print?
const a = [1, 2, 3, 4];
a.splice(1, 2);
console.log(a);1,42,31,2,3,41,2Hint : Start at that index and take out that many.
Exercise 12 β Quiz: Take the First Two
Copy the first two members without modifying the team.
Hint : One method copies without touching, and the end index is excluded.
Exercise 13 β Quiz: Joined
What does this code print?
console.log(["a", "b", "c"].join("-"));a-b-c-a-b-c-abca,b,cHint : The separator sits between the elements only.
Exercise 14 β Quiz: Split Pieces
What does this code print?
console.log("a-b-c".split("-").length);3251Hint : Count the pieces, not the cuts.
Exercise 15 β Quiz: Readable List
Print the names separated by a comma and a space.
Hint : One method builds a string, and the separator includes the space.
Exercise 16 β Quiz: Sorted as Text
What does this code print?
const nums = [5, 40, 300];
nums.sort();
console.log(nums);300,40,55,40,300300,5,4040,300,5Hint : Without a comparison function, values are compared character by character.
Exercise 17 β Quiz: Numbers Properly
What does this code print?
const nums = [5, 40, 300];
nums.sort((a, b) => a - b);
console.log(nums);5,40,300300,40,55,300,4040,5,300Hint : This comparison sorts from smallest to largest.
Exercise 18 β Quiz: Descending
Which comparison sorts from largest to smallest?
(a, b) => b - a(a, b) => a - b(a, b) => a + b(a, b) => a > bHint : Swapping the two names flips the direction.
Exercise 19 β Quiz: sort Modifies
What does this code print?
const a = [3, 1];
const b = a.sort((x, y) => x - y);
console.log(a);1,33,1undefinedHint : This method changes the array it is called on.
Exercise 20 β Quiz: Sort a Copy
Sort a copy, leaving the original alone.
Hint : One symbol builds a separate array first.
Exercise 21 β Quiz: Flattened
What does this code print?
console.log([[1, 2], [3]].flat().length);3214Hint : Add up the sizes of the inner arrays.
Exercise 22 β Quiz: flatMap
What does `r` contain?
const team = [
{ moves: ["a", "b"] },
{ moves: ["c"] }
];
const r = team.flatMap((c) => c.moves);["a", "b", "c"][["a", "b"], ["c"]]3[]Hint : It transforms, then opens one level.
Exercise 23 β Quiz: Array.from
What does this code print?
console.log(Array.from({ length: 3 }, (e, i) => i + 1));1,2,30,1,231,2,3,4Hint : The index starts at 0, then the function adjusts it.
Exercise 24 β Quiz: Which Family?
Which method modifies the array it is called on?
pushmapfiltersliceHint : Three of them return a new array instead.
Exercise 25 β Quiz: Untouched by filter
What does this code print?
const a = [1, 2, 3];
a.filter((n) => n > 2);
console.log(a.length);3120Hint : Did that method change anything, or return something new?