Calculate Number from a loop increment number

1579 views javascript
2

+---+---+---+
| 1 | 0 | 0 |
+---+---+---+
| 2 | 1 | 0 |
+---+---+---+
| 3 | 2 | 0 |
+---+---+---+
| 4 | 0 | 1 |
+---+---+---+
| 5 | 1 | 1 |
+---+---+---+
| 6 | 2 | 1 |
+---+---+---+
| 7 | 0 | 2 |
+---+---+---+
| 8 | 1 | 2 |
+---+---+---+
| 9 | 2 | 2 |
+---+---+---+

The code I am trying

var loop = 1;
while(loop < 10) {
    console.log(loop, loop%3, "I can't calculate this")
    loop++;
}

I have a loop increment variable loop and it counting 1,2,3,4,5....

I need to calculate 2 number from incremental variable: one is rounding 0,1,2 (loop % 3) I can do this but other 0,0,0 or 1,1,1

I mean I need to hold a number according to my round number.

answered question

Your code is JavaScript -- why are you using misleading java and python question tags? This will only invite down-votes.

You may ans in python code

I just need the logic to calculate the number

programming language doesn't matter

1 Answer

6

Fixed your code ((loop-1)%3) and added a division, rounding down:

var loop = 1;
while(loop < 10) {
    console.log(loop, (loop-1)%3, Math.floor((loop-1)/3))
    loop++;
}

posted this

Have an answer?

JD

Please login first before posting an answer.