What is the output of the following? “`js const nums = [1, 2, 3, 4, 5]; const result = nums.reduce((acc, n) => n % 2 === 0 ? […acc, n * 2] : acc, []); console.log(result); “`

JavaScript Professional Medium

JavaScript Professional — Medium

What is the output of the following? “`js const nums = [1, 2, 3, 4, 5]; const result = nums.reduce((acc, n) => n % 2 === 0 ? […acc, n * 2] : acc, []); console.log(result); “`

Key points

  • The reduce method is used to iterate over the array and accumulate values
  • The ternary operator checks if the number is even before multiplying by 2
  • The initial value of the accumulator is an empty array

Ready to go further?

Related questions