Skip to content
Home » How to Generate a Range of Numbers in JavaScript?

How to Generate a Range of Numbers in JavaScript?

This blog post will guide you on how to generate a range of numbers in JavaScript using a custom class called _Array. By leveraging the power of arrays and mapping, you can easily create a sequence of numbers within a specified range and step value.

Code

class _Array<T> extends Array<T> {
  static range(from: number, to: number, step: number): number[] {
    return [...Array(Math.floor((to - from) / step) + 1)].map(
      (v, k) => from + k * step
    );
  }
}
_Array.range(0, 9, 1);

Code Explanation

The provided code snippet introduces a custom JavaScript class called _Array that extends the built-in Array class. This custom class contains a static method called range which generates a range of numbers based on the specified inputs: from, to, and step.

Here’s an explanation of how the code works:

class _Array<T> extends Array<T> {
  static range(from: number, to: number, step: number): number[] {
    return [...Array(Math.floor((to - from) / step) + 1)].map(
      (v, k) => from + k * step
    );
  }
}

1. The _Array class is defined as a generic class that extends the Array class. This allows us to inherit all the functionalities of the Array class and add our additional methods or properties.

2. The range method is declared as a static method, which means it can be called directly on the class without creating an instance of it.

3. The range method takes three parameters: from, to, and step. These parameters define the starting point, ending point, and the interval of the number range, respectively.

4. Within the range method, an array of the appropriate length is created using the spread operator ([...Array(length)]). The length of the array is calculated by subtracting from from to, dividing it by step, and then flooring the result.

5. The generated array is then mapped using the map method, which accepts a callback function with two parameters: v (the current value) and k (the index). The callback function calculates each value in the range by adding from to the index multiplied by step.

6. Finally, the range method returns the array of generated numbers.

To use this custom _Array class and generate a range of numbers, you can simply call the range method with the desired from, to, and step values. Here’s an example:

_Array.range(0, 9, 1);

In this example, the range of numbers from 0 to 9 (inclusive) with a step of 1 will be generated. The result will be an array containing the numbers [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

By utilizing this simple and flexible solution, you can easily generate number ranges in JavaScript without the need for complex loops or manual calculations.

Remember to adjust the from, to, and step values according to your specific requirements when using the _Array.range method.

This approach can be extremely useful in various scenarios, such as generating sequences for data visualization, creating iterative loops, or any situation where a range of numbers is needed.

So, start leveraging the power of the _Array class to effortlessly generate number ranges in JavaScript and simplify your coding processes.

——————————————————————————————————————————

Also checkout the following codes.


How to Toggle Checkboxes in a HTML Table with jQuery
“How to Implement a Simple Payment System using Axios in JavaScript”