DEV Community

Jan Küster
Jan Küster

Posted on

One-Liner Puzzle - Fill Array with indices


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]


  • 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.


  • 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!

Top comments (0)