Range RightLoading saved progress…

Range Right

Implement rangeRight([start=0], end, [step=1]) — it produces the same set of numbers as Lodash's _.range, but the array comes back in descending order. The mental shortcut: build the ascending range [start, end) stepped by step, then reverse it. The signature mirrors range exactly, including the one-argument shorthand where the lone value is the end and start defaults to 0.

Signature

// start: number  — inclusive lower bound. Defaults to 0.
//                   With ONE argument, that argument is `end` and start is 0.
// end:   number  — exclusive upper bound. The range is half-open: [start, end).
// step:  number  — the increment between values. Defaults to 1, or to -1 when
//                  end < start (a descending source range).
// returns: number[]  — the [start, end) values, stepped by step, REVERSED.
function rangeRight(start, end, step): number[];

Examples

rangeRight(4);          // → [3, 2, 1, 0]   (one arg: end=4, start=0)
rangeRight(1, 5);       // → [4, 3, 2, 1]   (start..end form)
rangeRight(0, 20, 5);   // → [15, 10, 5, 0] (explicit step)
rangeRight(0, -4, -1);  // → [-3, -2, -1, 0] (negative step descends, then reverses)
rangeRight(0);          // → []              (empty range)

Notes

  • One argument is the end. rangeRight(4) means start = 0, end = 4. Two arguments are start, end. Three add an explicit step.
  • The range is half-open. end is never included: rangeRight(1, 5) covers 1, 2, 3, 4 and stops before 5.
  • Step sign is inferred when omitted. If end < start and no step is given, the step is -1 so the source range descends. Match this before reversing.
  • Define step 0 as []. Lodash emits a run of start for a zero step; here, return an empty array instead and the tests check that choice.
  • It is range reversed. Whatever range(start, end, step) would produce, rangeRight returns the same values in the opposite order.
Loading editor…