Scriptdrill

Level 1: Basics Exercise 9 of 45 Pro

Range of numbers

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Return the numbers from `start` up to, but not including, `end`, counting by `step`. A negative `step` counts down (then `end` is below `start`). If the step never reaches `end`, or `step` is 0, return an empty array.

What this exercise teaches

The loop condition flips with the direction of step, so the same code counts up and down, and a range that points the wrong way stops immediately. The early return for a step of 0 prevents an infinite loop.

A hint if you get stuck

Use a loop whose condition depends on the sign of `step`: `x < end` going up, `x > end` going down. Return early when `step === 0`, or the loop never ends.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Basics