The main problem with your code is that the expression n / loop
is different in each iteration. You might have intended that this expression was constant, in which case the logic would have been more reasonable.
Use a separate variable that starts with the value of n
and then is the subject of the subtractions, but without changing the original value of n that is used in the expression n / loop
:
function check(a, b) {
let loops = a / b;
let n = 200; // don't change n after this.
let n2 = n; // use a separate variable for that.
for (let i = 0; i <= loops; i++) {
console.log(Math.round(n2)); // only round in output
n2 -= n / loops; // use n, but only change n2
}
}
check(60,10);
2
solved Reducing a number on each iteration until it reaches exactly 0