DEV Community

loading...

One-Liner Puzzle - Fill Array with indices

jankapunkt profile image Jan Küster ・1 min read

Premise

Write a function f, that receives an integer n and returns a new Array of size n and which contains in each entry the index i. For example:

f(0) => []
f(3) => [0, 1, 2]
f(5) => [0, 1, 2, 3, 4]

Constraints

  • all code in one line
  • max allowed bytes/characters is 36
  • no underscore/lodash/libraries, just vanilla JS
  • ES6 is recommended
  • assume all inputs of n to be greater or equal 0 and lower than Number.MAX_SAFE_INTEGER
  • no performance considerations for this puzzle

Spoiler alert

If you like to solve on your own, please avoid the comment section until you solved it or (hopefully not) give up.

Hints

  • you should not target for "clean" code
  • omit variable declarations
  • remove any whitespace where you can
  • start with a working implementation and narrow down
  • if you are totally new to this appraoch, try my introduction to one-liners

Have fun!

Discussion (0)

pic
Editor guide