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(', '));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.