Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
How can I create a method to /2 the array

I am learning to code and I am trying out this javascript object method course. I am currently stuck on this method. I want to have the array with three different numbers(2,5,10) to be /2. I do not understand why it is returning NaN. Thank you for reading.

//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());
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try using .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;
        }
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe try .forEach to apply the same function to each element in an array

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda