Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function looping issue

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;
}

http://pastebin.com/2wi4Tnn8

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.

like image 293
NAME__ Avatar asked Jan 13 '23 22:01

NAME__


1 Answers

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.

like image 119
Dek Dekku Avatar answered Jan 15 '23 12:01

Dek Dekku