如何创建一个方法来 /2 数组
How can I create a method to /2 the array
我正在学习编码,并且正在尝试这个 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());
也许尝试 .forEach 将相同的函数应用于数组中的每个元素
const pokemonGoCommunityDay = {
eventBonuses: {
calculateEggHatchTime() {
const halfEggsTime = allEggsTime.forEach(egg=>egg/2)
return halfEggsTime;
}
}
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
尝试使用 .map()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
const pokemonGoCommunityDay = {
eventBonuses: {
calculateEggHatchTime() {
const halfEggsTime = allEggsTime.map(egg=>egg/2)
return halfEggsTime;
}
}
}
我正在学习编码,并且正在尝试这个 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());
也许尝试 .forEach 将相同的函数应用于数组中的每个元素
const pokemonGoCommunityDay = {
eventBonuses: {
calculateEggHatchTime() {
const halfEggsTime = allEggsTime.forEach(egg=>egg/2)
return halfEggsTime;
}
}
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
尝试使用 .map()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
const pokemonGoCommunityDay = {
eventBonuses: {
calculateEggHatchTime() {
const halfEggsTime = allEggsTime.map(egg=>egg/2)
return halfEggsTime;
}
}
}