Abe Miessler Abe Miessler - 1 year ago 587
Javascript Question

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

metricLine = utilities.div("metricline")
metricLine.grab utilities.sprite(icon, "metric_icon")
metricLine.grab utilities.div("metriclabel", label + ":")
metricLine.grab utilities.div("metricvalue", value)


But it throws the following error to my browser:

Uncaught SyntaxError: Illegal continue statement

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download