1

How do I get the below for loop statement to go through my array more than once?

var clients = new Array("client1", "client2", "client3", "client4", "client5");

var indexCounter;
var holmesminutes = 0;
for (indexCounter = 0; indexCounter < clients.length; indexCounter++) {
    holmesminutes = holmesminutes + 15;
    document.write(clients[indexCounter] + " testroom " + holmesminutes +"<br>");
}
4
  • why can you just run it again? Commented Jan 18, 2014 at 14:33
  • BTW, don't use new Array, use an array literal. Commented Jan 18, 2014 at 14:34
  • Is your issue that it only goes through one index of the array, or it goes through the WHOLE array once? Commented Jan 18, 2014 at 14:34
  • 1
    If you want it to run, say 2 times, look at agconti's answer below. If you want it to run infinite time, use a while(true){} block and have something inside your while(true){} block to escape an infinite loop Commented Jan 18, 2014 at 14:36

2 Answers 2

4

Put another loop around the loop.

for (var repeatCounter = 0; repeatCounter < 5; repeatCounter++) {
    for (indexCounter = 0; indexCounter < clients.length; indexCounter++) {
        holmesminutes = holmesminutes + 15;
        document.write(clients[indexCounter] + " testroom " + holmesminutes +"<br>");
    }
}

To stop all the loops when holmesminutes reaches 315:

for (var repeatCounter = 0; repeatCounter < 5 && holmesminutes < 315; repeatCounter++) {
    for (indexCounter = 0; indexCounter < clients.length && holmesminutes < 315; indexCounter++) {
        holmesminutes = holmesminutes + 15;
        document.write(clients[indexCounter] + " testroom " + holmesminutes +"<br>");
    }
}

As you see, you can put any condition you want in the test clause of for, it doesn't have to refer only to the iteration variable.

Sign up to request clarification or add additional context in comments.

5 Comments

I just have to know -- what part of this isn't totally obvious? You clearly know how to write loops, so what was it that you didn't get?
I've only just started to learn JavaScript I've never put a loop around another loop before. I've only just figured out when I need the loop to stop. Is there a way to stop the loop after the variable "holmesminutes" gets to 315? Thanks for your help.
What about other programming languages you've used before? Don't they have loops similar to JS?
I don't know any other programming languages, I've used c# a bit but part from that, I haven't done any others. I've only learnt html and CSS so far.
Sorry, I assumed that because you were so specific, you didn't mean you just started programming.
1

Maybe try a while loop:

var clients = new Array("client1","client2","client3","client4","client5");
var indexCounter;
var holmesminutes =0;
_i= 0;

while (_i < 2) {
    for(indexCounter = 0; indexCounter<clients.length;indexCounter++)
     {
     holmesminutes = holmesminutes + 15;
     document.write(clients[indexCounter] + " testroom " +  holmesminutes + "<br>");
     _i++;
     }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.