Skip to content
Home » Program to Generate a Range of Numbers with Step and Starting Value

Program to Generate a Range of Numbers with Step and Starting Value

This program demonstrates a function, called Range, that generates a range of numbers with a specified step and starting value. It is a useful tool for creating sequences of numbers in a flexible and efficient manner.

Code

const Range = function* (total = 0, step = 1, from = 0) {
  for (let i = 0; i < total; yield from + i++ * step) {}
};

Array.from(Range(5, -2, -10));
//=> [-10, -12, -14, -16, -18]

[...Range(5, -2, -10)]; // Five Elements With Step -2 From -10
//=> [-10, -12, -14, -16, -18]

// Also works with for..of loop
for (i of Range(5, -2, 10)) console.log(i);
// 10 8 6 4 2

// Lazy loaded way
const number0toInf = Range(Infinity);
number0toInf.next().value;
//=> 0
number0toInf.next().value;
//=> 1
// ...

Code Explanation

The provided code snippet consists of a generator function, named Range, that takes three parameters: total (the total number of elements in the range), step (the increment or decrement value for each element), and from (the starting value of the range).

Here’s how the code works:

1. The Range function is defined using the generator function syntax, indicated by the asterisk (*) after the function keyword.

2. Inside the Range function, a for loop is used to iterate over the range of numbers. The loop runs for a number of times specified by the total parameter.

3. In each iteration of the loop, the yield statement is used to generate the next value in the range. The value is calculated by adding the current index (i) multiplied by the step value to the starting value (from).

4. The function execution pauses after each yield statement until the next value is requested. This lazy loading approach allows for efficient memory usage when dealing with large ranges.

To demonstrate the usage of the Range function, several examples are provided:

1. The Array.from method is used to convert the Range generator into an array. The parameters passed to the Range function specify a range of 5 elements, with a step of -2, starting from -10. The resulting array is [-10, -12, -14, -16, -18].

2. The spread operator (…) is used to directly retrieve the values from the Range generator without converting it into an array. The parameters remain the same as the previous example, resulting in the same array [-10, -12, -14, -16, -18].

3. The for…of loop is used to iterate over the Range generator. The parameters are modified to specify the same range as before, but with a positive step and starting from 10. The loop outputs the values 10, 8, 6, 4, and 2.

4. Lastly, a new instance of the Range generator is created without specifying the total parameter. The next() method is called on the generator instance multiple times to retrieve consecutive numbers from 0 to Infinity.

By utilizing the Range function, you can easily generate ranges of numbers with custom step values and starting points, allowing for more flexibility and control in your programming tasks.

Implementing a program like this can save time and effort by automating the generation of number sequences, making it a valuable tool for various programming scenarios.

Remember that programs like these provide efficient and flexible solutions, making them beneficial for both students and working professionals in their day-to-day coding tasks.

Also checkout the following codes.


How to Use the Range2 Class in JavaScript for Generating Number Ranges?
How to Use the Range2 Function to Generate Number Ranges in JavaScript