I am trying to create a function, that will always total the mx variable, in the code:
(function () {
    var i = 0;
    var j;
    var mn = 1;
    var mx = 579;
    while (i < mx) {
        j = Math.floor(Math.random() * (mx - mn + 1)) + mn;
        mx = mx - i;
        i += j;
        if (i < (mx - j)) {
            console.log(i);
        } else {
            console.log(mx);
        }
    }
})();
So if mx = 999 then the console may log 900, 90 and 9 or if mx = 50 the console could log 4, 1, 10, 33 and 2.
This happens most of the time, but every now-and-again I get numbers that equal greater than mx and I can't for the life of me figure out why!
I'd be very grateful for any help, pointers or direction! As I'd like to lean where I went wrong, an explanation would be really awesome too!
Thank you!
 
     
    