gpt4 book ai didi

javascript - 将 const 更改为数组

转载 作者:行者123 更新时间:2023-12-02 21:32:59 25 4
gpt4 key购买 nike

这是对此处编写的代码的轻微扩展:product of a sequence (factorial) javascript

以下代码(请参阅代码片段)计算连续的乘积并跟踪 var b 的 2 个值的累积和,但如果我要在 var b 数组中添加更多数字并添加更多cumsum,则会变得相当长(例如cumsum2cumsum3、..)和nHourCum(例如nHourCum2、nHourCum3、..) .)

我想将 const cumsumconst nHourCum 写为数组。这个想法是避免添加cumsum1,cumsum 2。我尝试写:

const cumsum = [cumsum0 + b[0] * nHour, cumsum0 + b[1] * nHour];

nHourCum 也类似:

const nHourCum = [cumsum[0] + nHour, cumsum[1] + nHour]

而不是

const cumsum = cumsum0 + b[0] * nHour;
const cumsum1 = cumsum0 + b[1] * nHour;
const nHourCum = cumsum + nHour;
const nHourCum1 = cumsum1 + nHour;

但是这个方法似乎不起作用。

我对 const 数组做错了什么?

var a = document.getElementById("a");
var b = [1000000, 2000000];
let time0 = 1;
let cumsum0 = 0;
for (var n = 2; n <= 96; n++) {
const nHour = time0 * (959 - 10*n)/(960 - 10*n)
const cumsum = cumsum0 + b[0] * nHour;
const cumsum1 = cumsum0 + b[1] * nHour;
const nHourCum = cumsum + nHour;
const nHourCum1 = cumsum1 + nHour;
if (n==2)
{a.innerHTML += 'Hour ' + n + ': ' + b[0] * nHour + ' -- ' + (b[0] + b[0] * nHour) + ' ---- ' + b[1] * nHour + ' -- ' + (b[1] + b[1] * nHour) +'<br>';}
else
{a.innerHTML += 'Hour ' + n + ': ' + b[0] * nHour + ' -- ' + (b[0] + nHourCum) + ' ---- ' + b[1] * nHour + ' -- ' + (b[1] + nHourCum1) +'<br>';}
time0 = nHour;
cumsum0 = cumsum;
}
<body>
<p id="a"></p>
</body>

最佳答案

看起来您想这样做:

const a = document.getElementById("a");
const b = [1000000, 2000000];
let nHour, cumsum, nHourCum, time0 = 1, cumsum0 = 0;

for (let n = 2; n <= 96; n++) {
nHour = time0 * (959 - 10 * n) / (960 - 10 * n);
cumsum = [cumsum0 + b[0] * nHour, cumsum0 + b[1] * nHour];
nHourCum = [cumsum[0] + nHour, cumsum[1] + nHour];

if (n == 2) {
a.innerHTML += 'Hour ' + n + ': ' + b[0] * nHour + ' -- ' + (b[0] + b[0] * nHour) + ' ---- ' + b[1] * nHour + ' -- ' + (b[1] + b[1] * nHour) + '<br>';
} else {
a.innerHTML += 'Hour ' + n + ': ' + b[0] * nHour + ' -- ' + (b[0] + nHourCum[0]) + ' ---- ' + b[1] * nHour + ' -- ' + (b[1] + nHourCum[1]) + '<br>';
}
time0 = nHour;
cumsum0 = cumsum[0];
}

但我实在不明白为什么。正如您帖子下的评论中所指出的,您不能重新分配或重新声明常量,但是您可以更改可变对象,即使它们被分配给常量变量。老实说,您最好更改数组,而不是在每个循环中用新的变量重新分配一个变量:

const a = document.getElementById("a");
const b = [1000000, 2000000];
const cumsum = [0, 0];
const nHourCum = [0, 0];
let time0 = 1, cumsum0 = 0, nHour;

for (let n = 2; n <= 96; n++) {
nHour = time0 * (959 - 10 * n) / (960 - 10 * n);
cumsum[0] = cumsum0 + b[0] * nHour;
cumsum[1] = cumsum0 + b[1] * nHour;
nHourCum[0] = cumsum[0] + nHour;
nHourCum[1] = cumsum[1] + nHour;
if (n == 2) {
a.innerHTML += 'Hour ' + n + ': ' + b[0] * nHour + ' -- ' + (b[0] + b[0] * nHour) + ' ---- ' + b[1] * nHour + ' -- ' + (b[1] + b[1] * nHour) + '<br>';
} else {
a.innerHTML += 'Hour ' + n + ': ' + b[0] * nHour + ' -- ' + (b[0] + nHourCum[0]) + ' ---- ' + b[1] * nHour + ' -- ' + (b[1] + nHourCum[1]) + '<br>';
}
time0 = nHour;
cumsum0 = cumsum[0];
}
<p id="a"></p>

关于javascript - 将 const 更改为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60572357/

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