Created
September 24, 2009 15:55
-
-
Save subtleGradient/192818 to your computer and use it in GitHub Desktop.
Number.prototype.divideToArray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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