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'

1
  • Have you tried break instead of continue? Perhaps continue can only jump to a label that is on a loop statement. Commented Jun 18, 2011 at 3:28

4 Answers 4

14

The label should come immediately before the loop

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

iCantThinkOfAGoodLabelName:
for(i in circles) {
3
  • In that case, any suggestion how I could then generate the X and Y values would be very helpful
    – JJJollyjim
    Commented Jun 18, 2011 at 3:28
  • edited - there's no reason you can't generate them before the label. The presence of the label doesn't affect those lines in any way.
    – Brad Mace
    Commented Jun 18, 2011 at 3:29
  • 2
    Any reason why this wouldn't work? I've followed the instructions, but I'm just getting the same error. Commented Oct 10, 2013 at 12:34
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 = [];
    }
}
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.

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); 
      }
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.