JSFiddle - React, Tailwind, and code Playground

by Valentin Sarychev

JavaScript

function RangeArray(diffs) {
    this.diffs = diffs;
}

// Omit range by index
RangeArray.prototype.omitAt = function(index) {
    var arr = this.diffs;
    
    // move value to the next item in array
    arr[index + 1] += arr[index];
    arr[index] = 0;
}

// Find index of range for value
RangeArray.prototype.indexOf = function(value) {
    var arr = this.diffs;
    var sum = 0;
    
    for (var index = 0; index < arr.length; ++index) {
        sum += arr[index];
        if (value < sum) {
            return index;
        }            
    }
    
    return -1;
}

// ------- tests ----------

// array of ranges created using your percentage steps
var ranges = new RangeArray([25,25,20,15,10,5]);

// your random values
var values = [1, 26, 51, 70, 86, 99];

// test resutls: indexes of ranges for values
var indexes;

console.log('ranges: 1-25, 26-50, 51-70, 71-85, 86-95, 96-100');
console.log('random values: ' + values.join(', '));

// for your random values indexOf should return 0, 1, 2, 3, 4, 5 accordingly
indexes = values.map(function(x) { return ranges.indexOf(x); });
console.log('test 1 results: ' + indexes.join(', '));

// omit range at index 1 and 4
ranges.omitAt(1);
ranges.omitAt(4);

// for your random values indexOf should return 0, 2, 2, 3, 5, 5 accordingly
indexes = values.map(function(x) { return ranges.indexOf(x); });
console.log('test 2 results: ' + indexes.join(', '));