Split a range of number to a specific number of intervals JavaScript

In this problem statement, our task is to write a function that splits a range of numbers into a specific number of intervals using JavaScript. We need to provide the start value, end value, and the number of intervals.

Understanding the Problem

The problem requires creating a function that divides a numerical range into equal intervals. The inputs are the starting number, ending number, and desired number of intervals. The output should be an array of subarrays, where each subarray represents an interval with its start and end values.

For example, if the range is from 0 to 100 and we need 5 intervals, the function should return 5 subarrays. Each interval will have a size of 20, and every subarray will contain the start and end numbers of that specific interval.

Algorithm

Step 1: Create a function splitRange with three parameters: rangeStart, rangeEnd, and numIntervals.

Step 2: Calculate the interval size by dividing the range by the number of intervals.

Step 3: Create an empty array to store the intervals.

Step 4: Use a loop to calculate start and end points for each interval.

Step 5: Push each interval as a subarray into the intervals array.

Step 6: Return the array containing all intervals.

Example Implementation

// Function to split the range into intervals
function splitRange(rangeStart, rangeEnd, numIntervals) {
    const intervalSize = (rangeEnd - rangeStart) / numIntervals;
    const intervals = [];

    for (let i = 0; i < numIntervals; i++) {
        const start = rangeStart + i * intervalSize;
        const end = start + intervalSize;
        intervals.push([start, end]);
    }
    return intervals;
}

// Example usage
const rangeStart = 0;
const rangeEnd = 100;
const numIntervals = 5;
const intervals = splitRange(rangeStart, rangeEnd, numIntervals);

console.log("Split intervals:");
console.log(intervals);
Split intervals:
[ [ 0, 20 ], [ 20, 40 ], [ 40, 60 ], [ 60, 80 ], [ 80, 100 ] ]

Example with Different Range

// Example with a different range
const result = splitRange(10, 50, 4);
console.log("Range 10-50 split into 4 intervals:");
console.log(result);

// Example with decimal intervals
const result2 = splitRange(0, 10, 3);
console.log("Range 0-10 split into 3 intervals:");
console.log(result2);
Range 10-50 split into 4 intervals:
[ [ 10, 20 ], [ 20, 30 ], [ 30, 40 ], [ 40, 50 ] ]
Range 0-10 split into 3 intervals:
[ [ 0, 3.3333333333333335 ], [ 3.3333333333333335, 6.666666666666667 ], [ 6.666666666666667, 10 ] ]

Enhanced Version with Rounded Values

// Enhanced function with optional decimal precision
function splitRangeRounded(rangeStart, rangeEnd, numIntervals, precision = 2) {
    const intervalSize = (rangeEnd - rangeStart) / numIntervals;
    const intervals = [];

    for (let i = 0; i < numIntervals; i++) {
        const start = parseFloat((rangeStart + i * intervalSize).toFixed(precision));
        const end = parseFloat((start + intervalSize).toFixed(precision));
        intervals.push([start, end]);
    }
    return intervals;
}

// Example with rounded values
const roundedIntervals = splitRangeRounded(0, 10, 3, 2);
console.log("Rounded intervals:");
console.log(roundedIntervals);
Rounded intervals:
[ [ 0, 3.33 ], [ 3.33, 6.67 ], [ 6.67, 10 ] ]

Complexity Analysis

Time Complexity: O(n), where n is the number of intervals. The loop iterates exactly n times to calculate each interval.

Space Complexity: O(n), where n is the number of intervals. The function creates an array containing n subarrays, making the space usage proportional to the number of intervals.

Conclusion

This solution provides an efficient way to split numerical ranges into equal intervals in JavaScript. The function handles both integer and decimal ranges with O(n) time and space complexity, making it suitable for most practical applications.

Updated on: 2026-03-15T23:19:00+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements