JS Built-ins
← Array14 / 226
ArrayArray.fromintermediate

Array.from() β€” generate even numbers with a map function

πŸ“– Usage Example
Array.from([1, 2, 3], n => n * n)
// β†’ [1, 4, 9]

Pass a mapping function as the second argument to Array.from() to transform each element.

Array.from() β€” with a mapping function

The second argument to Array.from() is a mapping function applied to each element as the array is built. This lets you combine creation and transformation in a single step.

Challenge: Implement createEvenNumbers that returns the first count positive even numbers: [2, 4, 6, …, count*2].

createEvenNumbers(5) // β†’ [2, 4, 6, 8, 10]
createEvenNumbers(1) // β†’ [2]
πŸ’‘ Hints (2)
  • β€’ Use index `i` inside the map function: `(i + 1) * 2` gives 2, 4, 6…
  • β€’ The first argument `{ length: count }` controls how many elements are generated.