gpt4 book ai didi

JavaScript 间隔循环

转载 作者:行者123 更新时间:2023-12-03 07:59:15 25 4
gpt4 key购买 nike

循环如何像这样进行:

我的变量是这样的:

var array = [1,2,3,4,5,6,7,8,9,10]
var maximunNumbers = 6
var eachRound = 2

我想打印这样的数字:

In round 1 : print array[0] and array[1]
In round 2 : print array[2] and array[3]
In round 3 : print array[4] and array[5]
In round 4 : print array[0] and array[1] //numbers repeat themselves from here
In round 5 : print array[2] and array[3]
and so on ...

最佳答案

var array = [1,2,3,4,5,6,7,8,9,10];
var maximunNumbers = 6;
var eachRound = 2;


var i, k;
for( i = 0; i < 10; ++i ) {
var result = 'In round ' + i + ' : ';
for( k = 0; k < eachRound; ++k ) {
var array_index = ( ( i * eachRound ) + k ) % maximunNumbers;
result += ' array[' + array_index + '] = ' + array[array_index];
}
console.log( result );
}

In round 0 : array[0] = 1 array[1] = 2

In round 1 : array[2] = 3 array[3] = 4

In round 2 : array[4] = 5 array[5] = 6

In round 3 : array[0] = 1 array[1] = 2

In round 4 : array[2] = 3 array[3] = 4

In round 5 : array[4] = 5 array[5] = 6

In round 6 : array[0] = 1 array[1] = 2

In round 7 : array[2] = 3 array[3] = 4

In round 8 : array[4] = 5 array[5] = 6

In round 9 : array[0] = 1 array[1] = 2

https://jsfiddle.net/shctc5eu/

关于JavaScript 间隔循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34661915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com