Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal continue statement?

I have something similar to the following code:

_.each data, (value, type) ->     switch type         when "answered"             icon = "results_answered"             label = definitions.results.metrics.ta         when "leftblank"             icon = "results_lb"             label = definitions.results.metrics.tlb         when "standing"             icon = "results_standing"             label = definitions.results.metrics.cs         when "minimum"             icon = "results_min"             label = definitions.results.metrics.lowest         else             continue      metricLine = utilities.div("metricline")     metricLine.grab utilities.sprite(icon, "metric_icon")     metricLine.grab utilities.div("metriclabel", label + ":")     metricLine.grab utilities.div("metricvalue", value)     metricContainer.grab(metricLine)  metricContainer 

But it throws the following error to my browser:

Uncaught SyntaxError: Illegal continue statement

Is it possible to include a continue like I am trying without throwing the error?

like image 537
Abe Miessler Avatar asked Feb 12 '13 17:02

Abe Miessler


People also ask

Can you use continue in a forEach loop?

To continue in a JavaScript forEach loop you can't use the continue statement because you will get an error. Instead you will need to use the return statement in place of the continue statement because it will act in the same way when using a forEach because you pass in a callback into the forEach statement.

What is continue in if statement?

The continue statement passes control to the next iteration of the nearest enclosing do , for , or while statement in which it appears, bypassing any remaining statements in the do , for , or while statement body.

What is continue statement in JavaScript?

The continue statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.

How do you continue in for each?

Use return For practical purposes, return in a forEach() callback is equivalent to continue in a conventional for loop. When you return , you skip the rest of the forEach() callback and JavaScript goes on to the next iteration of the loop.


1 Answers

If you want to continue with the next loop iteration, you want return, not continue, as what you're passing to each is a function.

In a comment you mentioned being familiar wih the C# foreach loop, hence wanting to use continue. The difference is that wih C#'s foreach, you're dealing with an actual loop construct, whereas each is actually calling a function for each loop iteration, so it's not (at a language level) a loop, so you can't continue it.

like image 196
T.J. Crowder Avatar answered Sep 29 '22 06:09

T.J. Crowder