11

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'

karim79
  • 339,989
  • 67
  • 413
  • 406
JJJollyjim
  • 5,837
  • 19
  • 56
  • 78
  • Have you tried `break` instead of `continue`? Perhaps `continue` can only jump to a label that is on a loop statement. – Mark Eirich Jun 18 '11 at 03:28

4 Answers4

14

The label should come immediately before the loop

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

iCantThinkOfAGoodLabelName:
for(i in circles) {
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
7

Because iCantThinkOfAGoodLabelName: needs to be right before the loop.

iCantThinkOfAGoodLabelName:
for (blah; blah; blah)
    ..

I think what you want is a function..

function iCantThinkOfAGoodFunctionName() {
    var 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
            iCantThinkOfAGoodFunctionName();
        }
        thisCircle = [];
    }
}
McKayla
  • 6,879
  • 5
  • 36
  • 48
3

There should not be any statement between a label name and associated loop.

x = genX(radius);
y = genY(radius);
iCantThinkOfAGoodLabelName:
    for(i in circles) {

fixes it.

karim79
  • 339,989
  • 67
  • 413
  • 406
1

I recently had this issue and I resolved it by using all lowercase in the loop's label in version v0.8.x of Node.js.

Using labelname: vs. iCantThinkOfAGoodLabelName: might help you.

Others have correctly corrected you on the location of the label. It should be immediately before the for loop.

The Mozilla Developer Network on labels advises to avoid using labels, and instead prefer calling functions or throwing an error. You might rethink your strategy on using them, if possible.

Example of calling a function depending on result:

var i, j;

for (i = 0; i < 3; i++) {
   for (j = 0; j < 3; j++) {   
      if (i == 1 && j == 1) {
         // we want to ignore and continue
      } else {
         // do work with these variables from inside the doWork function
         // (scoped to whatever scope `this` for loop is in)
         doWork.call(this, i, j); 
      }
   }
}
slickplaid
  • 1,411
  • 1
  • 17
  • 20