假设我有8个项目的序列,这些循环使得在8之后变为1。
1,2,3,4,5,6,7,8
如果所选项目为数字7,我如何获得前三位数,即:2?
我有:
var total; // (total number in sequence)
var pos; // (current position number)
if ( pos < total) {
threeIncrement = pos+3;
} else {
threeIncrement = ??????
}
答案 0 :(得分:3)
var items = [1, 2, 3, 4, 5, 6, 7, 8];
var pos = 7;
var threeIncrement = (pos + 3) % items.length;