Reputation: 2837
I'm implementing a sort, and I've run into some unexpected behavior:
var searches = ['beta', 'alpha'];
var i = 0; j = 0;
for(i = 0; i < searches.length; i++){
min = i;
// first time through, i = 0
alert(i);
for(j = i; j<searches.length; j++);
{
// first time through j = 2. If i = 0, how does j = 2?
alert(j);
// .. sort code
}
}
In fact, j is always 2. Why isn't j being set to i when it enters the for loop?
Here's the jsfiddle: http://jsfiddle.net/w2kK9/3/
Upvotes: 0
Views: 84
Reputation: 96810
You have a misplaced semi-colon:
for (j = i; j < searches.length; j++); // <--
The rest is interpreted as a block that runs after the loop has executed (when j == 2
).
Take that out and it works fine.
Upvotes: 6
Reputation: 8511
Your nested for loop isn't doing anything:
for(j = i; j<searches.length; j++); // <- Your for loop proceeds only on this line.
Remove the semi colon.
Upvotes: 2