How do I split a number into chunks of max 100 in JavaScript?

Viewed 4032

I have a number (let's say 525). I would like to take this number and split it into an array of chunks with a max of 100 each value. If I took 525 and split it into an array, it would look like:

[
    100,
    100,
    100,
    100,
    100,
    25
]

Here's what I've tried so far:

var number = 525;
var array = [];
while (number > 0) {
    number = number - 100;
    array.push(Math.min(number, 100));
}

That doesn't get me far. It just returns [ 100, 100, 100, 100, 25, -75 ]. I know that using while isn't the best way to go, but that is what I could think of off the top of my head. Does anyone have any other way that improve my code and be more efficient?

6 Answers
Related