The program is this: Insert a number, for example 5 and add it with all numbers below it. In this case, it would be 5+4+3+2+1=15
I have this:
var res = 0
function addUp3(num) {
for (var i = 0; i <= num; i++) {
console.log(i);
res += i;
console.log(res);
}
}
I have no clue why it prints out random numbers.
The output of this was: 0,0,1,1,2,3,3,6 (without commas)
I've tried looking for an answer online, but they all want to do it with arrays, I don't think it's neccesary to do so. Can someone open my mind a bit please.
The for loop adds i each time. 0 + 1 + 2 + ... n. The reason it's printing twice is because you have 2 console.logs.
If you just want the final answer, have a console.log(res) after your for loop
var res = 0
function addUp3(num) {
for (var i = 0; i <= num; i++) {
res += i;
}
console.log(res)
}