Range

Medium
2
33.3% Acceptance

In this coding challenge, you will be implementing a range(start, end, step) function for JavaScript that creates an array of numbers progressing from a given start value up to, but not including, an end value.
The range function should take up to three arguments: start, end, and step. If step is not specified, it should default to 1.
In this challenge you only need to consider the incremental range case, meaning that the start is always less or equal to the end and a step is a positive number.

Instructions

  • Assume that start is always less or equal to end.
  • If a valid range cannot be generated, return an empty array.

Example test cases

const range1 = range(1, 5); console.log(range1); // [1, 2, 3, 4] const range2 = range(0, 10, 2); console.log(range3); // [0, 2, 4, 6, 8] const range3 = range(10, 0, -2); console.log(range4); // [10, 8, 6, 4, 2]

Hints

  • Firstly, you should make sure that the inputs to the function meet the specified criteria, if they do not, then return an empty array.
  • Make sure that a step is a positive number.
  • Once you're confident that your inputs are valid, create a new array and generate the range array by writing a for… loop and pushing the relevant numbers to the array.