Sign in

Libre University uses your GitHub account. Signing in is only needed to sit a final test, so the score is kept on your profile.

Map, filter and reduce

1.[3p]

What does ["1", "2", "3"].map(parseInt) produce, and why?

Correct
The answer is: `[1, NaN, NaN]`, because `map` passes the index as a second argument and `parseInt` reads it as the number base
The answer is: `[1, NaN, NaN]`, because `map` passes the index as a second argument and `parseInt` reads it as the number base
The answer is: `[1, NaN, NaN]`, because `map` passes the index as a second argument and `parseInt` reads it as the number base

2.[1p]

What does [1, 2, 3, 4].reduce((total, v) => total + v, 0) return?

CorrectNot quite: 10

3.[3p]

What happens when reduce is called on an empty array with no initial value?

Correct
The answer is: It throws a `TypeError`
The answer is: It throws a `TypeError`
The answer is: It throws a `TypeError`

4.[3p]

Match each method to the shape of what it returns.

  • map

  • filter

  • reduce

  • find

  • some

  • one value of any type

  • a boolean

  • an array of the same length

  • an array no longer than the original

  • the first matching element, or undefined

Show the answer

map: an array of the same length filter: an array no longer than the original reduce: one value of any type find: the first matching element, or undefined some: a boolean

5.[3p]

[4, 7, 10, 3, 8].reduce((n, v) => v > 5 ? n + 1 : n) is written without an initial value. What does it return?

CorrectNot quite: 7

6.[2p]

Filtering before mapping can change the answer, not merely the speed, because a predicate written for the original values does not mean the same thing applied to transformed ones.

Correct
The answer is: True

7.[3p]

In which of these cases is a plain loop the better choice?

Select all that apply

Correct
Correct
Correct
The answer is: The walk should stop as soon as a condition is met, The minimum, the maximum and the sum are all wanted from one pass, Each element is compared with the one before it

8.[2p]

Put these pipeline steps in the order that computes the total value of the paid orders.

  1. .reduce((sum, a) => sum + a, 0)

  2. .filter(o => o.paid)

  3. .map(o => o.amount)

Show the answer

c, b, a

9.[2p]

What initial value should a reduce that multiplies numbers together be given? Write the number.

CorrectNot quite: 1