3 ms·
Good hint! However, JavaScript labels via break/continue have subtle differences to classic GOTO statements. From the MDN site examples: var itemsPassed =
by vog 9y ago
Good hint! However, JavaScript labels via break/continue have subtle differences to classic GOTO statements.
From the MDN site examples:
var itemsPassed = 0;
var i, j;
top:
for (i = 0; i < items.length; i++) {
for (j = 0; j < tests.length; j++) {
if (!tests[j].pass(items[i])) {
continue top;
}
}
itemsPassed++;
}
The statement "continue top" does not really jump to top, because it only increases "i" and rechecks the loop condition.
In contrast "GOTO top" would re-initialize the loop ("i = 0") and effectively restart it from the very beginning.
- kristopolous 9y agoWell sure, but you're just doing it wrong. Here's the goto equivalent. var itemsPassed = 0; var i, j; top: do { for (i = 0; i < items.length; i++) { for (j = 0; j < tests.length; j++) { if (!tests[j].pass(items[i])) { continue top; } } itemsPassed++; } break; } while(1); Not a great pattern but there you go.
- microcolonel 9y agoYou can just pass 0 to while, and ditch the break. ;- )
- kristopolous 9y agoNo you can't. Try it if you don't believe me
- microcolonel 9y agoOh, forgot the weird label behaviour in JS, you're right. Funny to have the label up top, but the branching condition somehow in its way.