Reputation: 633
While working on a syntax highlighter that runs on JQuery, I have found a rather odd issue. A function I created appears to be almost corrupting any loop it is placed in. However, outside of loops, it works perfectly fine.
The function in question is:
function findQuoted(s)
{
var Quote = 0;
var F = 0;
var L = 0;
var Strings = Array();
for(i = 0;i < s.length;i++)
{
if(s.charAt(i) == '"' && Quote == 0)
{
Quote = 1;
F = i;
}
else if(s.charAt(i) == '"' && Quote == 1)
{
Strings[Strings.length] = s.substring(F, i + 1);
Quote = 0;
}
}
return Strings;
}
If this is executed in any loop, for some odd reason, the loop just ceases to work, and runs only once.
In this example, the alert messages are displayed only once each before continuing the program. Keep in mind, the program never gets stuck or goes non-responsive, the loop just ceases to function.
for(i = 0;i < 5;i++)
{
alert(findQuoted('"Test" this is a test "test" another test "TEST"'));
alert('test');
}
In a normal scenario, without this function being used, everything in this loop is executed 6 times. Due to this function being present in the loop, however, everything in this function is only executed one time.
Upvotes: 4
Views: 97
Reputation: 1461
Use
for(var i = 0;i < 5;i++)
otherwise you're using the same variable i
that you're using to iterate over in your other loop.
Upvotes: 9