Map, filter and reduce
1.[3p] What does ["1", "2", "3"].map(parseInt) produce, and why?
What does ["1", "2", "3"].map(parseInt) produce, and why?
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?
What does [1, 2, 3, 4].reduce((total, v) => total + v, 0) return?
3.[3p] What happens when reduce is called on an empty array with no initial value?
What happens when reduce is called on an empty array with no initial value?
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.
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?
[4, 7, 10, 3, 8].reduce((n, v) => v > 5 ? n + 1 : n) is written without an initial value. What does it return?
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.
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.
The answer is: True
7.[3p] In which of these cases is a plain loop the better choice?
In which of these cases is a plain loop the better choice?
Select all that apply
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.
Put these pipeline steps in the order that computes the total value of the paid orders.
.reduce((sum, a) => sum + a, 0).filter(o => o.paid).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.
What initial value should a reduce that multiplies numbers together be given? Write the number.