Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created September 24, 2009 15:55
Show Gist options
  • Select an option

  • Save subtleGradient/192818 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/192818 to your computer and use it in GitHub Desktop.
Number.prototype.divideToArray
Number.prototype.divideToArray = function(divideBy,forceInt){
var array = [];
var remainder = this % divideBy;
var result = this / divideBy;
if (forceInt)
result = (this - remainder) / divideBy;
for (var i = divideBy; i; i--)
array.push(result);
if (forceInt)
array[0] += remainder;
return array;
};
console.assert( (5).divideToArray(2)[0] === 2.5 , 'Should divide a number by the first argument');
console.assert( (5).divideToArray(2).length === 2 , 'Should divide a number into an array the length of the first argument');
console.assert( (5).divideToArray(2,true)[0] === 3 , 'Should add the remainder to the first item in the array when the second argument is true');
console.assert( (5).divideToArray(2,true)[1] === 2 , 'Should add the remainder to the first item in the array when the second argument is true');
console.assert( (5).divideToArray(2)[0] != 3 , 'Should NOT add the remainder to the first item in the array when the second argument is NOT true');
console.assert( (5).divideToArray(2)[1] != 2 , 'Should NOT add the remainder to the first item in the array when the second argument is NOT true');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment