Ad

Implement a function named generateRange(start, stop, step), which takes three arguments and generates a range of integers from start to stop, with the step. The first integer is value of the start, the second is the end of the range and the third is the step. (start may be less then stop (then step < 0)

Note: step != 0

Task
Create a function named

generateInterval(2, 10, 2) // [2,4,6,8,10]
generateInterval(2, -10, -2) // [2,0,-2,-4,-6,-8,-10]
Code
Diff
  • function generateInterval(start, stop, step) {
      const arr = [];
      let value = start;
      for (let index = 0; index <= (stop - start) / step; index++) {
          arr.push(value);
          value += step;
      }
      return arr;
    }

Implement a function named generateRange(start, stop, step), which takes three arguments and generates a range of integers from start to stop, with the step. The first integer is value of the start, the second is the end of the range and the third is the step. (start may be less then stop (then step < 0)

Note: step != 0

Task
Create a function named

generateInterval(2, 10, 2) // [2,4,6,8,10]
generateInterval(2, -10, -2) // [2,0,-2,-4,-6,-8,-10]
function generateInterval(start, stop, step) {
  const arr = [];
  let value = start;
  for (let index = 0; index <= (stop - start) / step; index++) {
      arr.push(value);
      value += step;
  }
  return arr;
}