我试图给变量加1,等待循环的5次迭代,然后再次加1。如何在x增量后添加1到一个值?
例如:
(其中x = 10和系数是迭代次数)
(5x(10)) + (5x + 1(10)) + (5x + 1 + 1(10)),
等
下面的代码可以指望这些迭代(0, 1, 2, 3, 4, and then back to 0)
但重复之后,我不确定要在哪里添加1。
for(i = 0; i < input; i++){
output = i%5
}
我试图给变量加1,等待循环的5次迭代,然后再次加1。如何在x增量后添加1到一个值?
例如:
(其中x = 10和系数是迭代次数)
(5x(10)) + (5x + 1(10)) + (5x + 1 + 1(10)),
等
下面的代码可以指望这些迭代(0, 1, 2, 3, 4, and then back to 0)
但重复之后,我不确定要在哪里添加1。
for(i = 0; i < input; i++){
output = i%5
}
此代码的工作,如:5 *(10 + 0)+ 5×(10 + 1)+ 5 *(10 + 2)+ ... 5 *(10 + N)
"use strict";
let input = 100;
let iterationsAmount = 5;
let randomNumber = 10;
let value = 0;
let result = 0;
for (let i = 0; i < input; i++) {
if (!(i % iterationsAmount)) {
result += iterationsAmount * (randomNumber + value);
value++;
}
}
console.log(result);
// set a default number.
var num = 20;
function loopi(n){
// variable to assign number to.
var setNum = 0;
// loop through the default number of times.
for(var ii = 0; n > ii; ii++){
// if the iteration number is divisible by 5 the add one to the set number.
if(ii % 5 === 0){
setNum++;
}
}
return setNum;
}
console.log(loopi(num));
难道你们就不能前加1,然后执行循环? – Matt
你能提供更多的细节你需要什么? (5x(10))+(5x + 1(10))+(5x + 1 + 1(10)),等等 - 我们在这个字符串中做了什么? – zhuravlyov
哎呀,我的坏。它应该是(5x(10 + 1))。 5 =迭代量,x是变量,所以5 * x和10只是一个随机数。每次增加10的数量增加1 - 因此增加10 + 1 + 1,而不是。 –