JS what is this loop: / break loop notation? - javascript

I just came accros this code:
var indx, hash;
loop:
for (var i in config.users) {
if (config.users[i].email === dataValues.email) {
indx = i;
hash = config.users[i].hash;
break loop;
}
}
Is this valid code? what is "loop:" ? It's hard to google 'js loop:' without just seeing the regular for .. in/ while loops

Yes, this valid code.
loop here is label
The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to.
NOTE: JavaScript has NO goto statement, you can only use labels with break or continue.
var i, j;
loop1:
for (i = 0; i < 3; i++) { //The first for statement is labeled "loop1"
loop2:
for (j = 0; j < 3; j++) { //The second for statement is labeled "loop2"
if (i === 1 && j === 1) {
break loop1;
}
document.write("i = " + i + ", j = " + j + "<br />");
}
}

Related

Why return doesn’t break the loop and function?

I'm new to javascript and trying to build a triangle. Here is a piece of code.
function makeLine (length){
var line = "";
for (x = 1; x <= length; x++){
line += "* ";
}
return line + "\n";
}
function buildTriangle (length){
var x = ""
for (j = 1; j <= length; j++){
x += makeLine(j);
}
return x
}
console.log(buildTriangle(10))
It seems that return would break the loop and function.
Then why return doesn't break the function and continue to loop?
And why the result is completely different if i slightly change the second function to
function buildTriangle (length){
var x = ""
for (j = 1; j <= length; j++){
x = makeLine(j);
}
return x
}
The difference between your first function and 2nd, that in the first you used "+=" which adds the makeLine function to what allready in "x". In your 2nd function you used just "=" so you declare x to be that makeLine function, which in other words means that your overriding it.
The return statement will not break the loop because it's outside of the for statement's scope. It is set to return a string after the loop.
I think what's wrong with your code is the declaration of your for method on the BuildTriangle function. Take a look at it:
for (j = 1; j >= length; j++){
x += makeLine(j);
}
The way its written it will call the makeLine function when j is more than or equal to length. Since j starts as 1, it will never call the makeLine function. You can fix by inverting the sign, like this:
for (j = 1; j <= length; j++){
x += makeLine(j);
}

Stuck in an infinite for loop. What am I missing here?

So I have this for loop, I'm stuck in the second one, but If I am, I'd also be stuck in the first one, whats the problem here?
for(var i = 0; i < oldtds.length;i += 3)
{
var oldNameIndex = sameName(oldtds[i].innerHTML, nameList);
if(oldNameIndex != -1)
{
//This means the name already exists
//I need to combine the times, and remove the tr from the table for the second name
//this loop accesses all the times
for(var j = i + 1; j < oldtds.length; j += 3)
{
//This won't quite work, There are colons between them
var timerArray = oldtds[j].innerHTML.split(":");
timerArray.push(oldtds[oldNameIndex + 1].innerHTML.split(":"));
console.log(timerArray);
console.log("this is j " + j);
}
}
}

What's the difference between these For loops that makes one crash?

This is from Codecademy's Javascript lesson "Search Text For Your Name". The following works:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for ( i=0; i < text.length; i++ ){
if (text[i] === myName[0]) {
for (var j = i; j < i + myName.length; j++) {
hits.push(text[j])
}
}
}
However, when I replace i + myName.length with j + myName.length, it's crashing. In full:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for ( i=0; i < text.length; i++ ){
if (text[i] === myName[0]) {
for (var j = i; j < j + myName.length; j++) {
hits.push(text[j])
}
}
}
I'm not getting any errors when I run this, which led me to believe that it's just stuck in an infinite loop, except that when I place a console.log marker within the For loop in question, it doesn't print anything.
What's the reason for it crashing?
j < j + myName.length; j++
j never reaches the end. You're incrementing it, but you compare it against number that is always larger than itself (assuming myName.length is > 0). The conditions for the loop is always satisfied, causing it to run forever.
It crashes because it's an infinite loop.
Here is your second example, with the static variables converted to their values:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for (i = 0; i < 42; i++) {
if (text[i] === 'Z') {
for (var j = i; j < j + 5; j++) {
hits.push(text[j]);
}
}
}
Specifically, your inner for condition is causing the infinite loop:
for (var j = i; j < j + myName.length; j++) {
or with myName.length replaced by its value, 5:
for (var j = i; j < j + 5; j++) {
j will always be less than j + 5 so the loop continues without end, consuming memory until crash.

Unable to push values into list

I am unable to push any elements into an array using .push(). The code here is my attempt:
text = "fihdfhdkfhkdsfkjd";
var myName = "Anthony Pham";
var hits = [];
for(var i = 0; i < text.length; i+++) {
if (text[i] === myName[i]) {
for(var j = i; j <= (myName.length + i); j++) {
hits.push(text[j]);
}
}
}
I am using CodeAcedmy and is givng me an error that my second for loop is unable to push any values into hits. I have tried switching between myName[j] and text[j] in the hits.push() but still cannot make the program work right. What is wrong with my second for loop here?
Try this ..
for(var i = 0; i < text.length; i++) {
if (text[i] === myName[i]) {
for(var j = i; j <= (myName.length + i); j++) {
hits.push(text[j]);
}
}
}
Aside from the extra + in i+++, you have a logic problem. The only time text[i] === myName[i] would be true is when i equals 9 (the h in Pham). Then you have this loop:
for (var j = i; j <= (myName.length + i); j++) {
// j = i = 9
// j <= 12 + 9 = 21
hits.push(text[j]);
// hits = [h, a, m, undefined, undefined, undefined, ... , undefined ]
}
use charAt(), not array brackets.
Also, you're never checking the bounds on myName, and are reading past the end in both places it's referenced.

How do I a break a sub for loop without breaking the main for loop in javascript?

I have a for loop and inside that for loop I have another for loop with a condition for break. The problem is that when the sub for loop breaks the top-level loop also does it. So how do I a break a sub for loop without breaking the main for loop in javascript?
Use a break statement, thus:
var i, j, s = "";
for(i = 0; i < 5; ++i) {
s += "\ni = " + i;
for(j = 0; j < 999; ++j) {
s += "\nj = " + j;
if(j === 1) {
break;
}
}
}
Notice that at the end, s contains the result of five iterations of the whole loop, rather than one.
One possible cause for your issue might be an extra misplaced semicolon, thus:
var i, j, s;
s = "";
for(i = 0; i < 5; ++i) {
s += "\ni = " + i;
for(j = 0; j < 10; ++j); {
s += "\nj = " + j;
if(j === 10) {
break;
}
}
}
In the above case, the inner for loop is actually empty, but indentation makes it look as though it contains the following block (which is actually an immediate child of the "outer" loop). This means that the apparent "outer" loop will break on the first iteration.
Another cause might be a typo leading to the incorrect use of assignment vs equality, thus:
var i, j, s;
s = "";
for(i = 0; i < 5; ++i) {
s += "\ni = " + i;
for(j = 0; j < 10; ++j) {
s += "\nj = " + j;
if(i = 5) {
break;
}
}
}
In this situation, in the condition that causes the inner loop to break, the loop variable of the outer loop has been assigned a value that causes the outer loop to finish. Assignment is truthy, so the break will always be hit.
Yet another cause might be a misunderstanding about variable scope in javascript. In other languages, you might be able to do something like this:
var i, s = "";
for(i = 0; i < 5; ++i) {
s += "\nouter = " + i;
if(true) {
var i;
for(i = 0; i < 999; ++i) {
s += "\ninner = " + i;
if(i === 10) {
break;
}
}
}
}
The trouble here is that in javascript, variables are scoped to the function in which they are declared with var. (or global, if outside a function or used without var). This means that the i of the inner loop is actually the same i as the outer loop

Categories