我要计算该项目的“花费”总额。
baseCost = 32;
upgrade.baseCost + (32 * 0)
upgrade.baseCost + (32 * 1)
upgrade.baseCost + (32 * 2)
//...
upgrade.baseCost + (32 * 100)
total = ?;
输出应该是0到100之间的每笔“成本”,因此我可以算出升级上“花费”的总金额。所以我想添加upgrade.baseCost +(32 * 0)+ upgrade.baseCost +(32 * 1)+ upgrade.baseCost +(32 * 2)...任何帮助,感激不尽,谢谢!
答案 0 :(得分:1)
您需要迭代数字0到100,或者1到100。因此:
当务之急:
const baseCost = 42;
let total = 0;
for(let i = 0; i <= 100; i++) { // 0-100
total += upgrade.baseCost + 32 * i;
}
注意:您可以跳过0
的第一次迭代,因为这对您的总和没有影响。
一种更具功能性(ES6)的方式:
const total = Array.from(Array(100).keys()).reduce((accumulator, currentValue) => accumulator + upgrade.baseCost + (32 * (currentValue + 1)), 0);
// creates an array of length 100, zero-indexed so we need to add one to the current number while we add and at the same time avoiding baseCost * 0.
Array.from
:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
Array.keys
:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys
Array.reduce
:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
答案 1 :(得分:0)
我不太确定这是否是您想要的...但是您可以使用简单的for循环并根据需要执行它,并为每次迭代计算一个值,将其保存在变量中并在其中显示结束。
devtools::install_github("bnosac/pattern.nlp", INSTALL_opts = "--no-multiarch")