views:

86

answers:

4

I maybe doing something wrong, but this seems quite weird to me:

for(i=1; i < 5; i++){ alert(i) }

in before mentioned firefox version gives me five counts of i. the question simply is: what the hell?

thanks!

SOLUTION: ok, it was my own mistake. i actually had an alert after the one in the loop which displayed number 5 :) thanks all who were ready to help and sorry for wasting your time.

A: 

What is the first value of i? What will be the last value of i, according to the loop conditional of i < 5?

Jonathan Feinberg
the first value i 1. last value, if i < 5 should be 4. hence i expect 4 counts, right?
Andrej Marinic
+1  A: 

I pasted javascript:for(i=1; i < 5; i++){ alert(i) } into my Firefox 3.5.3 address bar and it correctly alerted four times, 1-4.

ceejayoz
yes, i get 5 counts. i was hoping it was a firefox issue, but since you guys are getting correct results, i'll have to search for other cause. thanks!
Andrej Marinic
A: 

It should only loop 4 times:

Granted yes, this is C++ code its the same concept:

#include <iostream>

using namespace std;

int main()
{
for (int i = 1; i < 5; i++)
{
cout << i << endl;
}
return 0;
}


$ ./test
1
2
3
4

I guess I am not understanding exactly what the question is.

Nathan Adams
i am getting 5 alerts. which is obviously not correct. i have assumed this was a firefox issue, since IE is behaving as expected. but since people above tried the code and it worked, i have to find other cause. thanks for your help though! :)
Andrej Marinic
Do you have any plugins or addons installed? I would try Firefox in safe mode: http://support.mozilla.com/en-US/kb/Safe+ModeI know the Skype plugin will insert its own Javascript code for making phone numbers click able on a page.
Nathan Adams
i have personas and firebug plugins. i thought of that as well, because it's really weird and the observation persists no matter what values i take. it simply overrides the < sign and takes the next value. anyway, i'll report again once i have turned the plugins off.
Andrej Marinic
+1  A: 

Does your browser, in the page where you're seeing this problem, also give you five counts when you use a variable name other than "i"? Does it do it when you explicitly declare a local "i" like this:

for (var i = 1; i < 5; ++i) alert(i);

?

Pointy
i will definitely check that when i'm back home. good idea :)
Andrej Marinic