[英]How can I loop through two sets of numbers?
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.我需要无限期地遍历两组数字,先一组,然后在每组之间暂停 10 秒,其中两个值 go 都返回 -1。 Both start at -1 and both go up to 2.
两者都从 -1 开始,并且都是 go 到 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
You could take an array and increment the value with a given index until a max value and change the index for incrementing the other inddex, and so on.您可以获取一个数组并使用给定索引递增该值直到最大值,然后更改索引以递增另一个索引,依此类推。
let data = [0, 0], index = 0; setInterval(() => { if (++data[index] > 2) { data[index] = 0; index = 1 - index; } console.log(...data); }, 500);
I would store those indices in an array instead of two variables.我会将这些索引存储在一个数组中,而不是两个变量中。 That way it is easily extensible to 3 or more indices.
这样它就很容易扩展到 3 个或更多索引。
Here is an implementation where there are 3 instead of 2 indices (to better demo the idea).这是一个实现,其中有 3 个而不是 2 个索引(为了更好地演示这个想法)。 At any start of the timer callback there will be only one index that is not equal to -1:
在定时器回调的任何开始,都只有一个不等于 -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.意识到这里使用了一个数组而不是两个(或更多)索引变量(
firstIndex
, secondIndex
),因为这是更好的做法。
So use indices[0]
where you would have used firstIndex
and indices[1]
where you would have used secondIndex
.因此,在本应使用
firstIndex
的地方使用indices[0]
,在本应使用secondIndex
的地方使用indices[1]
。
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);
As you added another scenario to your question, here are some observations:当您向问题添加另一个场景时,这里有一些观察结果:
i
that increments by 1, starting with 0i
i
.i
的一半决定。 When that half is even , we need to make the first index different from -1, otherwise the second index.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.i/4
(整数除法),正如我们所见,需要 4 次以上的“迭代”才能使同一索引获得递增一步的值。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.