DEV Community

Justin
Justin

Posted on • Edited on

JS Challenge: Create an array and fill with serial numbers

The following code creates and fills an array with numbers 0 to 9

[...Array(10).keys()]
Enter fullscreen mode Exit fullscreen mode

Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

If you want the numbers from 1 to 10:

[...Array(10).keys()].map(i => i+1)
Enter fullscreen mode Exit fullscreen mode

Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Or this should work as well:

Array(10).fill().map((v,i) => i+1)
Enter fullscreen mode Exit fullscreen mode

Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay