DEV Community

Jan Küster 🔥
Jan Küster 🔥

Posted on

1

One-Liner Puzzle - Fill Array with indices

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!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more