【发布时间】:2022-01-20 20:32:17
【问题描述】:
我正在学习编码,并且正在尝试这个 javascript 对象方法课程。我目前坚持这种方法。我想让具有三个不同数字(2、5、10)的数组为/2。我不明白为什么它会返回 NaN。感谢您的阅读。
//Eggs hatch time
eggHatchTime2km = 2
eggHatchTime5km = 5
eggHatchTime10km = 10
allEggsTime = [eggHatchTime2km,eggHatchTime5km,eggHatchTime10km];
console.log(allEggsTime); //reads out 2,5,10
const pokemonGoCommunityDay = {
eventBonuses: {
calculateEggHatchTime() {
return allEggsTime/2; //return NaN
//return eggHatchTime2km,eggHatchTime5km,eggHatchTime10km/2; //return the value of the last variable(10km) but not 2km and 5km
},
}
}
console.log(pokemonGoCommunityDay);
console.log(pokemonGoCommunityDay.eventBonuses.calculateEggHatchTime());
【问题讨论】:
-
您尝试在数组上使用
/,但/仅对数字有意义。您需要遍历allEggsTime中的每个项目并将它们相除,然后将结果推送到新数组中(或更新当前索引处的项目以保存新的计算值)
标签: javascript arrays object methods