I am having a nigger moment

I am having a nigger moment.
I know this equals ten but I am trying to understand the logic.
var count = 0;for (var i = 0; i < 5; i++) {count = count + i;}alert("count is" + count);
The for part is what's troubling me. i equals 0 so while i is less than five add one (i++)?
Wtf?

Other urls found in this thread:

8ch.net/bbbb/res/146.html#6329
twitter.com/SFWRedditImages

You have come to the right place.
Wanna be a vol?

for (var i = 0; i < 5; i++) {

is initializing i to 0, says to keep the loop running as long as i is less than 5, and to increment i (i.e. increase it by 1) at the end of each run through the loop.

but wouldn't that be
0+1=1
1+1=2
2+1=3
3+1=4
4+1=5
How does it come to ten?

im not good at math tbh

you cannot be serious? four iterations of triangle number

count = 0 + 0 (count is 0, i is 0)
count = 0 + 1 (count is 0, i is 1)
count = 1 + 2 (count is 1, i is 2)
count = 3 + 3 (count is 3, i is 3)
count = 6 + 4 (count is 6, i is 4)
i is incremented and is no longer less than 5
count is 10

That is definitely what I would expect to happen.

When $var is now 5, you should exit the for() loop. Is there more code you're working with? Does it run 6-10, or 1-5, or 1-10 …?

OS / compiler?

...

i++ uses the variable, then increments it. your code also adds one after that's done.
So it should say
Count is 2
Count is 4
Count is 6
Count is 8
Count is 10

because you're double incrementing it every iteration.

It's not my code, and it does no such thing. I is incremented by 1 once per loop in OP's code.

This one doesn't make sense either:
var tops = 5while (tops > 5) {for (var spins = 0; spins < 3; spins++) {alert("top is spinning!");}tops = tops - 1;}
I entered it into the console and it was 15 (that's the number of times I had to press ok).
This is the way I had worked it out:
tops=5 spins=0 +1
tops=4 spins=1 +1
tops=3 spins=2 +1
tops=2 spins=3 +1

or maybe this is the answer:
tops=5 spins=0 +1
tops=4 spins=1 +1
tops=3 spins=2 +1
tops=2 spins=3 +1
tops=1
adding the tops up come to 15. I dunno if that is the answer. I mean in the code it says the while the spins are less than 3 so I can not go beyond the spins = 3.

Who's in the pic? Looks familiar?

Was that you pestering me on skype last night?

Loop body does not execute. Not even once.
(tops > 5) evaluates to false.

Psst: 8ch.net/bbbb/res/146.html#6329

[how does code work niggers]
[nani

desu

ne]

[if niggers then
print tongue my anus
else print is coo fam
end]
[ mabye if
i type like this?]
[or
this?]
[ help
me]

[one line last try sorry fagets]

Good catch.
Correct code:
var tops = 5;while (tops > 0) { for (var spins = 0; spins < 3; spins++) { alert("Top is spinning!"); } tops = tops - 1;}