gpt4 book ai didi

javascript - 如何在javascript中返回一个整数乘以3的数组?

转载 作者:行者123 更新时间:2023-12-01 00:03:16 24 4
gpt4 key购买 nike

我认为我的问题可能是我的 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/

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