我目前正在编写一个返回NaN的函数。我发现只有一个错误来源。
以下是返回NaN的相关代码:
function test() {
Logger.log(howMuchDamage([[808, 404], [1814, 2595, 0], 0.3869, 0.6802, 0.103, 1], [100000, 10875, [0, 0], [0.1, 0.1], 0, 0], ["FT", "IE", [0, 0.2], 0.2075 * 3, 0.0208 * 3, 0, 0], [0.05], [0.05, 0.05], [0.2,0.05]))
}
function test2() {
Logger.log(tooltipNumber("FT", [0, 0.2], 0.2075 * 3, 0,0208 * 3, 0, 0, [1814, 2595, 0], [808, 404]))
}
function howMuchDamage(attackerStats, targetStats, abilityData, multiplicative, additive, critIncrease) {
var damage = 0
var crit = 0
var hit = didItHit(attackerStats[5], targetStats[2], abilityData[0])
if(hit === 1) {
damage = tooltipNumber(abilityData[0], abilityData[3], abilityData[4], abilityData[5], abilityData[6], attackerStats[1], attackerStats[0])
crit = didItCrit(attackerStats[3] + critIncrease[0], targetStats[4], abilityData[1])
if(crit === 1) {
damage *= criticalDamage(attackerStats[4] + critIncrease[1], attackerStats[3] + critIncrease[0])
}
else if (crit === -1) {
crit = 0
damage *= (1 - targetStats[5])
}
//Multiply damage by the multipliers through an array with all the relevant multipliers and apply DR.
damage *= multiplicativeMultiplier(multiplicative, additiveMultiplier(additive)) * (1 - totalDR(targetStats[2], targetStats[1], abilityData[1], abilityData[2]))
}
return [1, damage, crit, hit]
}
function tooltipNumber(type, coefficient, StdHPmin, StdHPdiff, AMP, bonus, weaponDamage) {
if(type === "MR") {
return coefficient * bonus[0] + (1 + AMP) * (Math.random() * weaponDamage[1] + weaponDamage[0]) + baseDamageValue * (Math.random() * StdHPdiff + StdHPmin)
}
else if(type === "FT") {
return coefficient * bonus[1] + baseDamageValue * (Math.random() * StdHPdiff + StdHPmin)
}
else if(type === "heal") {
return coefficient * bonus[2] + baseHealingValue * (Math.random() * StdHPdiff + StdHPmin)
}
else {
return coefficient * bonus[1] + baseDamageValue * (Math.random() * StdHPdiff + StdHPmin)
}
}
howMuchDamage
应该返回一个数字列表(除了损坏之外的所有内容的整数)。 damage
是NaN。
我已将问题缩减为damage = tooltipNumber(abilityData[0], abilityData[3], abilityData[4], abilityData[5], abilityData[6], attackerStats[1], attackerStats[0])
。我尝试将其更改为damage = 5000
,但仍然返回NaN。
tooltipNumber
也正在返回NaN。一切似乎都很好。只添加浮点数或整数。唯一的字符串是if
条件。
答案 0 :(得分:1)
在调查the link with all the code之后,我注意到了这个问题。
问题来自totalDR
。
当您致电时,您将targetStats[2]
作为damageReduction
传递
稍后使用此damageReduction
,将其添加到armorContribution
。
但是damageReduction
是一个数组([0, 0]
),所以你不能只做damageReduction + armorContribution
返回NaN
并需要修复..