I'm new to JavaScript and coding in general, and i'm running into a problem with for-loop. I tell the loop to repeat 5 times, and the loop is supposed to halve the given number(200) 5 times, resulting in 6.25. Currently it halves the 200 5 times, but I can't figure out how to save the number in variable after each loop correctly, so it just repeats 200/2 = 100, 200/2 = 100, 200/2 = 100, 200/2 = 100, 200/2 = 100.
How can I save result of each loop into variable and use the updated variable in the next run of the loop?
function toisto(t,a,o) {
//t = 5
//a = 200
//o is a function for halving
for (var i = 0; i < t; i++) {
var puolitustulo = o(a);
}
console.log(puolitustulo);
}
varand useletandconst(which would also "solve" your problem, because it then would correctly throw an error)