我认为我的问题可能是我的 i 值和 start 值相同......所以我的循环停止得太早。为了解决这个问题,我尝试将“while (i < length)”设置为“while (i < length + start)”,我这样做是为了添加回 start 参数的值,但我的输出没有改变。
function triple_sequence(start, length) {
var res = [start];
var i = start;
while (i < length) {
i *= 3;
res.push(i);
i++;
}
return res;
}
console.log(triple_sequence(2, 4));
//[2, 6, 18, 54]
最佳答案
您正在检查i
而不是结果的长度。改变
while (i < length)
至
while (res.length < length)
另外,不要将 i++
放入循环中。您已将 i
更新为 i *= 3;
。
关于javascript - 如何在javascript中返回一个整数乘以3的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60571033/