Jump to content

for loop inside a for loop with a break


Go to solution Solved by calmchess,

Recommended Posts

in the following code snippet I have a for loop within a forloop but I need to break the first for loop but the break seems to stop both for loops instead of just one. I can't put the second for loop outside the first forloop because trackUserNames0 is defined within the first for loop and will negatively impact the first for loop if I call it before the break......in short how do I nest a for loop and it completes when the first forloop breaks? Maybe I'm clear off base here. some advice is welcomed.

 


trackUserNames0[i].innerHTML=userName0;
for(var j=0;j<4;j++){
trackArr00[j].className=roomId0;
}
if(userName0=="offline"){
trackUserNames0[i].style.color="red";
}else{
trackUserNames0[i].style.color="black";
}
 
break;
 
}

 

--calmchess

Link to comment
https://forums.phpfreaks.com/topic/278822-for-loop-inside-a-for-loop-with-a-break/
Share on other sites

  • Solution

Nevermind I just learned something if you change a group of elements className within a for loop it changes the length of the elements dynamically while the for loop is running. I didn't call break on both elements one elements array just shrank so I was getting undesired results. Here is the entire working function in case somebody wants to study it.

 

 

change1pic:function(picpaths0,userName0,roomId0,userLength0){
setTimeout(delay,3000);
function delay(){
document.getElementsByClassName(roomId0)[0]=="admin"?trackArr00= document.getElementsByClassName(roomId0): trackArr00= document.getElementsByClassName("admin");
 
alert("this 6"+trackArr00.length);
for(var i=0;i<trackArr00.length;i++){//this loop is necessary! there are 4 pic containers!
 
//tracArr00[0] refers to the container that the 4 images are in
//get image containers
var trackPicCons0 = trackArr00[i].getElementsByTagName("img")[0].src.split("?")[0].substr(29,6);
//get username containers //there is only 1 cotainer for usernames //use 0
var trackUserNames0 = trackArr00[i].parentNode.getElementsByTagName("span");
//get link containers //set all pic containers links to the roomid //use i
var trackRoomLinks0 = trackArr00[i].parentNode.getElementsByTagName("a");
 
/*since admin has a shorter character length than defcon0 I don't need to change substr, 
it will parse as far as possible and return the shorter string*/
 
if(trackPicCons0=="defpic"||trackPicCons0=="admin"){
 
trackArr00[0].getElementsByTagName("img")[0].src = "http://localhost/book/images/"+picpaths0[0];
 
trackUserNames0[i].innerHTML=userName0;
for(var j=0;j<4;j++){
trackRoomLinks0[j].href="http://localhost/book/swf/connect.html?roomid="+roomId0;
}
if(trackPicCons0=="admin"){
for(var k=0;k<4;k++){
trackArr00[k].className=roomId0;
}
}
if(userName0=="offline"){
trackUserNames0[i].style.color="red";
}else{
trackUserNames0[i].style.color="black";
}
 
break;
 
}
 
}
}
 
},
 

 

--calmchess

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.