i was just refreshing my javascript doing some little exercise, and I’m doing sumAll exercise which is to make a function that takes 2 integers and returns the sum of every number between(and including) them.
for example:
sumAll(1, 4) // returns the sum of 1 + 2 + 3 + 4 which is 10
I know and have solve the exercise. it just that when im solving it, i was inputing something wrong. and I realized I don’t know why that it work that way.
This is the wrong code i made:
function sumAll(start,end) {
let total = 0;
for(let i = start; i<=end; i++) {
i++
}
return total;
}
when I do console.log(i++) it return the value of 2,5,undefined
My question is: why it return such value?
because as far as i know i++ mean i = i+1 and and the value for i here should be 1,2,3,4. I think im missing something here.
You’re not adding anything here, but just incrementing i by one and you are doing it twice
Here you start by making i be the value of start, and the loop goes on if i is less than or equal to end. Then you add 1 to i by using i++
Then the only thing you are doing here is adding one to i again. So you are adding to the i twice. You make the total variable, but you never use it again. Then you return it which is still 0 because you never added anything to the variable
No, i++ means add 1 to i.
sum += i would be sum = sum+i
I’m a little confused why and where you ran console.log(i++). This will print i and then increment i by 1.
As @Cody_Biggs mentioned, you never add to the total variable, so it should return 0.
Try this, it might be more clear what happens to i:
function sumAll(start,end) {
let total = 0;
for(let i = start; i<=end; i++) {
console.log(i)
console.log(i++, "i++") //print i and then add 1 to i
console.log(i)
i++ //add 1 to i
console.log(i)
}
return total;
}
console.log(sumAll(2,5))
Ah, right it’s i + 1. but i still don’t understand why the value of the console.log(i++) is 2,5
in a sense that:
why is that value of i in i++ is equal to start and end?
I thought value of i would be from start to end
I would take a look at the other response you were given again
I am not sure why you would want to console i++ in the first place, but your console is not checking i its checking i++ so you wouldnt see the range from start to end. If you want to see start to end you can see this code used here
You can click on console to see the values. Note that the 0 is coming from the total variable
Hello there,
i ran (console.log(i++) to see what value it was. this is not the right code to solve the exercise in the first place because the mistake i made. but I just want to understand how it runs the code. I’m aware i never add the total variable and would return 0. but that’s not the issue since, what i want to understand is the value of i++ that was returned.
now about the placement you mention, after i run few codes. now i realize I dont understand that it would affect the value that was return. Now i’m even more confused…
when i run your code, the result is: 2, 2 'i++', 3, 4, 5, 5 'i++', 6, 7
seems that’s mean the value of i++ is equal to start and end.
when i run this code:
function sumAll(start,end) {
let total = 0;
for(let i = start; i<=end; i++) {
i++
console.log(i++, 'i++')
}
return total;
}
console.log(sumAll(2,5))
what was return is 3 and 6. which is the value of 2 +1 and 5+1.
and when i run this code:
function sumAll(start,end) {
let total = 0;
for(let i = start; i<=end; i++) {
console.log(i)
console.log(i++, "i++") //print i and then add 1 to i
console.log(i)
i++ //add 1 to i
console.log(i)
console.log(i++, "i++")
}
return total;
}
console.log(sumAll(2,5))
what was returned is: 2, 2 i++, 3, 4, 4 i++
The question:
why if console.log(i++) placed before i++ it would have the same value as start and end but if after it would increment it by 1?
My guess is that because the console.log run before the i was incremented by one if put before i++ and i++ has been ran through if the console.log is placed after it.
regarding the last code that i made a little tweak. why the second console.log(i++, 'i++') that i put as the last console.log to be ran give the value of 4 i++ instead of 5 i++ or 6 i++? and after that it stop. unlike the code that you wrote which would return the value 5 until 7.
Hey there,
Seems the placement of console.log affecting it. I want to console i++ just to see the value, im aware it’s not checking i or checking the return value. this code is not the code to solve the exercise. I just want to understand how things work by tweaking here and there.
I made a little tweak from that code, which is:
function sumAll(start,end) {
let total = 0;
for(let i = start; i<=end; i++) {
console.log(i)
console.log(i++, "i++") //print i and then add 1 to i
console.log(i)
i++ //add 1 to i
console.log(i)
console.log(i++, "i++")
}
return total;
}
console.log(sumAll(2,5))
I’m kind of confused, why the second console.log(i++, 'i++') that i put as the last console.log to be ran give the value of 4 i++ instead of 5 i++ or 6 i++ ? and after that it stop. unlike the original code that would return the value 5 until 7.
Thanks! that’s really helpful! My problem was i thought console.log(i++) would print the value of i++ right there. apparently the value of i added by 1 would be printed at the next console.log(i).
one last question:
function sumAll(start,end) {
let total = 0;
for(let i = start; i<=end; i++) {
console.log(i)
console.log(i++, "i++") //print i and then add 1 to i
console.log(i)
i++ //add 1 to i
console.log(i)
console.log(i++, "i++")
}
return total;
}
console.log(sumAll(2,5))
this result stop at 4 i++ and when i add another console.log(i) it would return 5 after 4 i++.
I’m quite confused why it didn’t work like your code:
function sumAll(start,end) {
let total = 0;
for(let i = start; i<=end; i++) {
console.log(i)
console.log(i++, "i++") //print i and then add 1 to i
console.log(i)
i++ //add 1 to i
console.log(i)
}
return total;
}
console.log(sumAll(2,5))
which would return until 7.
when i put the code to the link you shared. it seems like your code ran the loop twice but mine only once.
Basically you just keep incrementing i all the time, unnecessarily. It probably went over end
for(let i = start; i<=end; i++) {
This is all you need, i will increment by one for each loop.
If you want to see the value of i during the loop use console.log(i).
If you use console.log(i++) it will print i (5) and then increment it again (i will now be 6)
function sumAll(start,end) {
let total = 0;
for(let i = start; i<=end; i++) {
console.log(i)
console.log(i++, "i++") //print i and then add 1 to i
console.log(i)
i++ //add 1 to i
console.log(i)
console.log(i++, "i++") //this increments i again
console.log(i)
}
return total;
}
console.log(sumAll(2,5))