0
Hours
0
Minutes
0
Seconds
You need Login/Signup to run/submit the code.

Implement a function to generate a range of numbers | Range I | JavaScript Interview Question | Lodash Polyfills

@Devtools Tech
161

In this question, the candidate needs to implement a function called range that creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end.

  • A step of -1 is used if a negative start is specified without an end or step.
  • If end is not specified, it's set to start with start then set to 0.

This method is similar to the _.range method provided by the popular library lodash.

Syntax

range(start, end, step);

Arguments

  • [start=0] (number): The start of the range. Optional argument.
  • end (number): The end of the range.
  • [step=1] (number): The value to increment or decrement by. Optional argument.

Returns

  • (Array): Returns the range of numbers.

Examples

range(4);
// => [0, 1, 2, 3]
 
range(-4);
// => [0, -1, -2, -3]
 
range(1, 5);
// => [1, 2, 3, 4]
 
range(0, 20, 5);
// => [0, 5, 10, 15]
 
range(0, -4, -1);
// => [0, -1, -2, -3]
 
range(1, 4, 0);
// => [1, 1, 1]
 
range(0);
// => []

Submission

Start the timer, complete your solution, test your solution against the test cases provided by the platform, and submit it. Ideally, you should finish this question within 30 mins.

Share your solution with us on twitter or linkedin.

Loading IDE...