Quizz

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

β–Ά Do the exercises

Exercise 1 β€” Quiz: At Least One

What does this code print?

console.log([4, 8, 12].some((l) => l >= 10));
Options
β€’ true
β€’ false
β€’ 12
β€’ 1

Hint : 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));
Options
β€’ false
β€’ true
β€’ 2
β€’ [12]

Hint : A single failure settles it.

Exercise 3 β€” Quiz: Empty every

What does this code print?

console.log([].every((x) => x > 0));
Options
β€’ true
β€’ false
β€’ undefined
β€’ An error

Hint : 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));
Options
β€’ 6
β€’ 0
β€’ 3
β€’ 123

Hint : 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));
Options
β€’ 103
β€’ 3
β€’ 100
β€’ 102

Hint : 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));
Options
β€’ 12
β€’ 2
β€’ 0
β€’ 48

Hint : 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));
Options
β€’ 20,30
β€’ 20,30,40
β€’ 10,20
β€’ 30

Hint : 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);
Options
β€’ 3
β€’ 1
β€’ 2
β€’ 0

Hint : 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);
Options
β€’ 1,4
β€’ 2,3
β€’ 1,2,3,4
β€’ 1,2

Hint : 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("-"));
Options
β€’ a-b-c
β€’ -a-b-c-
β€’ abc
β€’ a,b,c

Hint : The separator sits between the elements only.

Exercise 14 β€” Quiz: Split Pieces

What does this code print?

console.log("a-b-c".split("-").length);
Options
β€’ 3
β€’ 2
β€’ 5
β€’ 1

Hint : 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);
Options
β€’ 300,40,5
β€’ 5,40,300
β€’ 300,5,40
β€’ 40,300,5

Hint : 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);
Options
β€’ 5,40,300
β€’ 300,40,5
β€’ 5,300,40
β€’ 40,5,300

Hint : This comparison sorts from smallest to largest.

Exercise 18 β€” Quiz: Descending

Which comparison sorts from largest to smallest?

Options
β€’ (a, b) => b - a
β€’ (a, b) => a - b
β€’ (a, b) => a + b
β€’ (a, b) => a > b

Hint : 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);
Options
β€’ 1,3
β€’ 3,1
β€’ undefined
β€’ An error

Hint : 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);
Options
β€’ 3
β€’ 2
β€’ 1
β€’ 4

Hint : 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);
Options
β€’ ["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));
Options
β€’ 1,2,3
β€’ 0,1,2
β€’ 3
β€’ 1,2,3,4

Hint : The index starts at 0, then the function adjusts it.

Exercise 24 β€” Quiz: Which Family?

Which method modifies the array it is called on?

Options
β€’ push
β€’ map
β€’ filter
β€’ slice

Hint : 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);
Options
β€’ 3
β€’ 1
β€’ 2
β€’ 0

Hint : Did that method change anything, or return something new?

Back to the course