The problem with the work cycle

While working on a syntax shortcut that works in jQuery, I found a rather strange problem. The function I created seems to almost distort any loop in which it is placed. However, outside of loops, it works great.

This function:

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 runs in any loop, for some odd reason, the loop just stops working and starts only once.

In this example, warning messages are displayed only once before continuing the program. Keep in mind that the program never gets stuck or does not respond, the cycle simply stops functioning.

 for(i = 0;i < 5;i++) { alert(findQuoted('"Test" this is a test "test" another test "TEST"')); alert('test'); } 

In a normal scenario, without using this function, everything in this loop is executed 6 times. However, due to this function present in the loop, everything in this function is executed only once.

+4
source share
1 answer

Using

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

otherwise, you use the same variable i that you use to repeat in another loop.

+9
source

Source: https://habr.com/ru/post/1491183/


All Articles