Estoy aprendiendo a codificar y estoy probando este curso de método de objetos de JavaScript. Actualmente estoy atascado en este método. Quiero tener la matriz con tres números diferentes (2,5,10) para ser /2. No entiendo por qué está devolviendo NaN. Gracias por leer.
//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());Intenta usar .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; } } }Tal vez intente .forEach para aplicar la misma función a cada elemento en una matriz
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