Create an array of numbers in the given range
List the integers from min to max inclusive.
function range({ min, max }) { return Array.from({ length: max - min + 1 }, (_, i) => min + i); }
Input & expected output
Input
{
"min": 1,
"max": 5
}Expected output
[
1,
2,
3,
4,
5
]The edge case
Input
{
"min": 3,
"max": 3
}Expected output
[
3
]Know the limits
- The lab requires min ≤ max and at most 1,000 numbers before allocating.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Create an array of numbers in the given range from the original 1loc collection by Nguyen Huu Phuoc, with jonrandy, Tsvika10. Copyright (c) 2020 phuoc-ng. MIT licensed.
Uses Array.from with a length; validates the range size before allocating. Original MDX retained as inert source evidence.
Maintained by Oveyon. Original source commit: 14b481cf60df. This edition’s file: contents/create-an-array-of-numbers-in-the-given-range.mdx in akamind/1loc, commit 0a77f131b22d. Read the license.
Keep exploring
- Clone an array — Create a new array without copying the objects inside it.
- Remove duplicate values — Keep the first occurrence of each value, in its original order.
- Flatten a nested array — Turn a nested array into one sequence of values.