gpt4 book ai didi

javascript - 拼接和切片的可能错误/误解

转载 作者:行者123 更新时间:2023-11-30 14:21:36 27 4
gpt4 key购买 nike

我有下一个循环:

    rolling_average_delta_follower=[];
followers=[32,34,36,38,40,42,44,46,48,50,52,54,56] // .length = 12
delta_followers=[50,52,54,56,58,60,62,64,66,68,70,72,74] // leng= 12

for (i = 0; i < followers.length ; i++) {

copie = delta_followers.slice(0); //creates duplicate of array delta_followers so I keep source original and not cut from it
copie.splice(7,i) // supposed to create an array that contains numbers from 50 to 64 -> next time the for executes it should go 52 to 66 and so on
console.log(copie)
for (i = 0; i < 8; i++) { // the 7 numbers added previously in the one array are getting summed up
totalx += copie[i]
}
rolling_average_delta_follower.push(totalx) // the sum of each array previously created is getting added to the main array where I need the data.


}

一切都很好,直到我尝试实际执行它,我最终得到了一个我似乎无法逃脱的永远循环。

如有任何帮助,我们将不胜感激。

谢谢!

最佳答案

问题出在这里:

for (i = 0; i < 8; i++) {  // the 7 numbers added previously in the one array are getting summed up
totalx += copie[i]
}

通过这段代码,您可以覆盖上面循环中使用的 i。只需在这里使用另一个变量名。 (j ?)

关于javascript - 拼接和切片的可能错误/误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52656086/

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