简体   繁体   中英

How to write a for loop that will pick up a count where it left off?

I know this is very silly question, but I am struggling to find this logic. I am trying to work on this very basic for loop to achieve this result

0 - 0
0 - 1
0 - 2
0 - 3
0 - 4
0 - 5
0 - 6
0 - 7
0 - 8
0 - 9
0 - 10
0 - 11

1 - 12
1 - 13
1 - 14
1 - 15
1 - 16
1 - 17
1 - 18
1 - 19
1 - 20
1 - 21
1 - 22
1 - 23

2 - 24
2 - 25
2 - 26
2 - 27
2 - 28
2 - 29
2 - 30
2 - 31
2 - 32
2 - 33
2 - 34
2 - 35

The inner loop should continue from the number where the first inner loop was cut done. in the first iteration it left off at 11 , the second time it comes to the inner loop it should go from 12 - 24 and so forth.

var count = 0;
var val = 0;
for(i = 0; i < 3; i++) {
    for(j = 0; j < count + 12; j++) {
        console.log(i + " - " + j);       

    }
    val = j;
    count = count + j;
    console.log(count);
}

There are several "clever" answers here. I'd stick with a "simple to read and simple to debug" answer. Here's a solution in C# that should be simple enough to translate:

int k = 0;
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 12; j++)
    {
        Console.WriteLine(i + " - " + k++);
    }
    Console.WriteLine();
}

Organizational Skills Beat Algorithmic Wizardry

You don't need 2 loops, you can achieve this with a single loop:

for (var i = 0; i < 36; i++){
  console.log(Math.floor(i/12) + " - " + i);  
}

If you don't like Math.floor, you can use the double bitwise not operator to truncate the float:

for (var i = 0; i < 36; i++){
  console.log(~~(i/12) + " - " + i);  
}

One loop

You don't need two loops because you can use some simple math.

Use the modulo operator ( % ) to find the remainder of i divided by 12 , if there is no remainder, increment n , otherwise continue.

As 0 is technically a multiple of twelve, (0 is a multiple of everything) you need to start n at minus one.

 function demo(n, i) { document.body.innerHTML += n + ' ' + i + '<br>'; } var x = 12, y = 3, l = (x * y), n =-1; for(var i = 0; i < l; ++i) { if(i % x === 0) ++n; demo(n, i); } 

You can wrap it in a function definition to aid in reuse:

 function demo(n, i) { document.body.innerHTML += n + ' ' + i + '<br>'; } function loopMultiples(l, x, callback) { var n =-1 for(var i = 0; i < l; ++i) { if(i % x === 0) ++n; callback(n, i); } } loopMultiples((12*3),12, demo); 

The importance of algorithmic wizardry ;)

Two loops

If you want to use two loops, for whatever reason, it should look something like this:

 function demo(i, n) { document.body.innerHTML += i + ' ' + n + '<br>'; } var n = 0, x = 12, y = 3; for(var i = 0; i < y; ++i) { for(var j = 0; j < x; ++j) { demo(i, n++); } } 


The following is a response to the comment below. I can't think of a reason to use either of the following methods in normal production code (unless you're trying to confuse someone), but they do return the expected result.

Three loops

 function demo(i, n) { document.body.innerHTML += i + ' ' + n + '<br>'; } var n = 0, x = 6, y = 3, z = 2; for(var i = 0; i < y; ++i) { for(var j = 0; j < z; ++j) { for(var k = 0; k < x; ++k) { demo(i, n++); } } } 

Four!

 function demo(i, n) { document.body.innerHTML += i + ' ' + n + '<br>'; } var n = 0, w = 2, x = 3, y = 3, z = 2; for(var i = 0; i < y; ++i) { for(var j = 0; j < z; ++j) { for(var k = 0; k < z; ++k) { for(var l = 0; l < x; ++l) { demo(i, n++); } } } } 

This uses two loop statements, but, honestly, is still uses the same number of loops overall, no matter how you split it out (ie, two loops, looping 3 and 12 times each or one loop looping 36 times . . . 36 loops either way).

It's also takes parameters, to support different counts:

function doubleLoop(outerCount, innerCount) {
    for (i = 0; i < outerCount; i++) {
        var currentOffset = (i * innerCount);

        for (j = 0; j < innerCount; j++) {
            console.log(i + " - " + (currentOffset + j));
        }
    }
}

Then just call it with whatever "count" numbers that you need:

doubleLoop(3, 12);  //this would get you what you asked for in your question

Not near as clever as the first approach:

var majorCount = 3;
var minorCount = 12;
var counter = 0;

for(var i = 0; i < majorCount; i++) {
  for (var x = counter; x < counter + minorCount; x++) {
    console.log(i + " - " + x);
  }
  counter += minorCount;
}

If you dont want use two loop and think math.floor is too expensive.

http://jsfiddle.net/rdh5mv59/

var firstID = 0;
var RangeSize = 12;
for (i = 0; i < 36; i++) {
    if (i >= RangeSize * (firstID + 1)) {
        firstID++;
    }
    console.log(firstID + " - " + i);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM