Problem

Python has a range function that generates a sequence of numbers starting at the first argument, incrementing by the third argument, and stopping at the second argument.

range(0, 5) // [0, 1, 2, 3, 4, 5]
range(3, 9, 3) // [3, 6, 9]
range(10, 50, 10) // [10, 20, 30, 40, 50]

How would you recreate this in JavaScript?

Solution
range(0, 5) // [0, 1, 2, 3, 4, 5]
range(3, 9, 3) // [3, 6, 9]
range(10, 50, 10) // [10, 20, 30, 40, 50]

How would you recreate this in JavaScript?

const range = (start, stop, step=1) => {
  return Array.from(
    { length: (stop - start) / step + 1 },
    (value, i) => start + i * step
  )
}

The function uses Array.from() to generate an array of a specified length and fill it with values generated by the provided function. The length is calculated as (stop - start) / step + 1, which represents the number of elements in the desired range. The function passed to Array.from() generates each element by starting with start, adding an increment of i * step, where i is the index of the current element.