Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

illegal continue statement in javascript $.each loop

I am getting an error that this has an illegal continue statement. I have a list of words to check for form validation and the problem is it was matching some of the substrings to the reserved words so I created another array of clean words to match. If it matches a clean word continue else if it matches a reserved word alert the user

$.each(resword,function(){         $.each(cleanword,function(){             if ( resword == cleanword ){                 continue;             }             else if ( filterName.toLowerCase().indexOf(this) != -1 ) {                 console.log("bad word");                 filterElem.css('border','2px solid red');                 window.alert("You can not include '" + this + "' in your Filter Name");                 fail = true;             }         });     }); 
like image 754
BillPull Avatar asked Aug 11 '11 15:08

BillPull


People also ask

What is continue in for loop JavaScript?

The continue statement breaks one iteration (in the loop) if a specified condition occurs, and continues with the next iteration in the loop. The difference between continue and the break statement, is instead of "jumping out" of a loop, the continue statement "jumps over" one iteration in the loop.

Can we use continue statement outside loop?

No, continue can not be used outside loops.

How do you skip a loop in JavaScript?

The continue statement (with or without a label reference) can only be used to skip one loop iteration. The break statement, without a label reference, can only be used to jump out of a loop or a switch.

Is there a Continue statement in JavaScript?

The continue statement in JavaScript is used to jumps over an iteration of the loop. Unlike the break statement, the continue statement breaks the current iteration and continues the execution of next iteration of the loop. It can be used in for loop, while loop, and do-while loop.


1 Answers

The continue statement is fine for normal JavaScript loops, but the jQuery each method requires you to use the return statement instead. Return anything that's not false and it will behave as a continue. Return false, and it will behave as a break:

$.each(cleanword,function(){     if ( resword == cleanword ){         return true;     }     else if ( filterName.toLowerCase().indexOf(this) != -1 ) {         //...your code...     } }); 

For more information, see the jQuery docs.

like image 148
James Allardice Avatar answered Sep 29 '22 21:09

James Allardice