What does this print?
const nums = [1, 2, 3, 4];
const result = nums
.filter(n => n % 2 === 0)
.map(n => n * 10)
.reduce((sum, n) => sum + n, 0);
console.log(result);Options
- 60 (Correct option)
- 100
- 20
- [20, 40]
Answer
60
Why
The chain runs step by step:
filter(n => n % 2 === 0)keeps even numbers: [2, 4].map(n => n * 10)multiplies each: [20, 40].reduce((sum, n) => sum + n, 0)adds them starting from 0: 20 + 40 = 60.
These three array methods (filter, map, reduce) never change the original array; they return new values.
Remember
filter keeps, map transforms, reduce combines.