Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this labeled javaScript continue not working?

I am using this code to weather some circles are overlapping:

iCantThinkOfAGoodLabelName:
x = genX(radius);
y = genY(radius);
for(i in circles) {
  var thisCircle = circles[i];
  if(Math.abs(x-thisCircle["x"])+Math.abs(y-thisCircle["y"])>radius*2) { //No overlap
    continue;
  } else { //Overlap
    continue iCantThinkOfAGoodLabelName; //<- Line 256
  }
  thisCircle = [];
}

But when the continue statement is reached, chrome's developer console gives me this: client.html:256 Uncaught SyntaxError: Undefined label 'iCantThinkOfAGoodLabelName'

like image 831
JJJollyjim Avatar asked Jun 18 '11 03:06

JJJollyjim


People also ask

How do you continue a loop in JavaScript?

The continue statement is used to skip the current iteration of the loop and the control flow of the program goes to the next iteration. The syntax of the continue statement is: continue [label]; Note: label is optional and rarely used.

How do you use labels in JavaScript?

You can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution. Note that JavaScript has no goto statement, you can only use labels with break or continue . In strict mode code, you can't use let as a label name.

What is continue 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.

Which of the following is correct way to define label in JavaScript?

JavaScript label statements are used to prefix a label to an identifier. A label can be used with break and continue statement to control the flow more precisely. A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code.


1 Answers

The label should come immediately before the loop

x = genX(radius);
y = genY(radius);

iCantThinkOfAGoodLabelName:
for(i in circles) {
like image 191
Brad Mace Avatar answered Nov 04 '22 17:11

Brad Mace