I’ve got two small puzzles that I can’t quite figure out!
I need to loop through two sets of numbers indefinitely, one first and then the other with a 10 second pause between each one in which both values go back to -1. Both start at -1 and both go up to 2.
firstIndex = -1 secondIndex = -1
I can loop through the first number using:
setInterval(() => { if (firstIndex === 2) { firstIndex = 0; } else { firstIndex++ } }, 10000);
But I can’t figure out how to switch between the values.
So the results would be…
-1, -1 (10 secs) starting values 0, -1 (10 secs) -1, -1 (10 secs) back to start 1, -1 (10 secs) -1, -1 (10 secs) back to start 2, -1 (10 secs) -1, -1 (10 secs) back to start -1, 0 (10 secs) -1, -1 (10 secs) back to start -1, 1 (10 secs) -1, -1 (10 secs) back to start -1, 2 (10 secs) -1, -1 (10 secs) back to start …and repeat
Please help 🙂
And if it’s possible (!) would it also be able to create a loop to switch from left to right like this…
-1, -1 (10 secs) starting values 0, -1 (10 secs) -1, -1 (10 secs) back to start -1, 0 (10 secs) -1, -1 (10 secs) back to start 1, -1 (10 secs) -1, -1 (10 secs) back to start -1, 1 (10 secs) -1, -1 (10 secs) back to start 2, -1 (10 secs) -1, -1 (10 secs) back to start -1, 2 (10 secs) -1, -1 (10 secs) back to start …and repeat
Advertisement
Answer
I would store those indices in an array instead of two variables. That way it is easily extensible to 3 or more indices.
Here is an implementation where there are 3 instead of 2 indices (to better demo the idea). At any start of the timer callback there will be only one index that is not equal to -1:
var max = 2; var indices = [-1, -1, max]; // put max in last entry setInterval(() => { let i = indices.findIndex(n => n !== -1); indices[i]++; if (indices[i] > max) { indices[i] = -1; indices[(i + 1) % indices.length] = 0; } console.log(...indices); }, 300);
The assignments in this code make the update to array elements. Realise that an array is used here instead of two (or more) index variables (firstIndex
, secondIndex
), as this is better practice.
So use indices[0]
where you would have used firstIndex
and indices[1]
where you would have used secondIndex
.
If you really, really want to use those to variables, then initialise them every time an update is made, like this (here we use just two indices instead of the three above):
var max = 2; var indices = [-1, max]; // put max in last entry setInterval(() => { let i = indices.findIndex(n => n !== -1); indices[i]++; if (indices[i] > max) { indices[i] = -1; indices[(i + 1) % indices.length] = 0; } let [firstIndex, secondIndex] = indices; console.log(firstIndex, secondIndex); }, 300);
Switching left and right
As you added another scenario to your question, here are some observations:
- We can use a helper variable
i
that increments by 1, starting with 0 - When that index is even (not odd), then the output should always be -1, -1
- When that index is odd, one of the indices should be different from -1.
- As that index toggles from side to the other, and this happens every two steps, the “side” (first or second) is determined by the half of
i
. When that half is even, we need to make the first index different from -1, otherwise the second index. - The actual value for that index should be
i/4
(integer division), as we can see that it takes 4 more “iterations” to have that same index get a value that is one step incremented.
Here is how that can be achieved:
var max = 2; var indices = [-1, -1]; var i = 0; // This is the state that determines the setInterval(() => { index = (i >> 1) % 2; if (i % 2 == 0) { // Every two times, output is -1 -1 indices[1 - index] = -1; } else { indices[index] = i >> 2; } let [firstIndex, secondIndex] = indices; console.log(firstIndex, secondIndex); i = (i + 1) % ((max + 1) * 4); }, 300);