Skip to content

Instantly share code, notes, and snippets.

@datafunk
Created October 31, 2014 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datafunk/1d5fa9bc4591c35f4f86 to your computer and use it in GitHub Desktop.
Save datafunk/1d5fa9bc4591c35f4f86 to your computer and use it in GitHub Desktop.
Check which bucket each individual array member belongs to
/*!
Check which bucket each individual values member belongs to
*/
var values = [22, -55.2, -18, -5, 0, 0.23, 5.1, 12, 450];
// classes is to verify my bucketing is correct
var classes = [];
// range values must fit inside
var buckets = [-10, 0, 0.2, 0.5, 10, 20];
var l = buckets.length;
// c is for counter
var c = 0;
// array_pos is the return value once a value is bucketed
var array_pos = 0;
function inRange(x, min, max) {
// console.log(x >= min && x <= max);
return x >= min && x <= max;
}
(function addInfinites(){
// a & z are lowest / highest integers in JS = Math.pow(2,53)
// == +/- 9007199254740992
var a = Math.pow(2,53) * -1;
var z = Math.pow(2,53);
// console.log(['buckets: ', buckets]);
buckets.unshift(a);
buckets.push(z);
// console.log(['buckets: ', buckets]);
})();
function repeater(val, c) {
if ( inRange(val, buckets[c], buckets[c+1]) == false ){
c++;
// console.log(['c:', c]);
repeater(val, c);
} else {
classes.push(val, c);
console.log('val: ' + val, 'is in between ' + buckets[c] + ' and ' + buckets[c+1], classes);
}
}
// this function is
for (var i in values ){
repeater(values[i], c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment