Read here. Code on desktop. The coding workspace opens on screens 1024px and wider.
JavaScript interview question
Range Right
easy10 min
Generate an integer range and return its values in the reverse order of the corresponding range sequence.
Requirements
Implement rangeRight(start = 0, end, step). With one bound, generate from 0 towards that bound. With no arguments, return [].
Include start and exclude end. Default step is 1 when start <= end and -1 otherwise. Explicit steps may be positive or negative.
Return the same values as range(start, end, step), in reverse order. Positive-step ranges are therefore descending in the result; negative-step ranges are ascending. Do not simply start at end.
Equal bounds or a step pointing away from end returns []. A zero step, fractional value, NaN, or Infinity in any supplied number also returns [].
Assume numeric arguments and valid ranges containing at most 10,000 values. Return a fresh array on every call. Do not modify global objects.
Examples
Example 1
Input: rangeRight(4)
Output: [3, 2, 1, 0]
Example 2
Input: rangeRight(2, 9, 3)
Output: [8, 5, 2]
Example 3
Input: rangeRight(-3)
Output: [-2, -1, 0]
Generate an end-exclusive integer sequence.
What the tests check
Supports a positive single bound
Supports start and end bounds
Supports steps that do not divide the interval
Defaults to negative steps for descending bounds
Supports explicit negative steps
Returns empty for equal bounds and no arguments
Returns empty when the step points away from end
Rejects invalid numbers and zero steps without looping