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

147
Visualizações
foreach Statements with a function

ok I am trying to answer a question and learning to code. regarding an array and using foreach statements the function must remain as this "function animalNames" must stay somewhere but apparently, I am doing something wrong because I get it returned as undefined. even through it produces the correct array back could someone look at it and let me know what i have done wrong.

attached is a picture of the code and array and question that i answered. this is how i wrote my function.

     const displayNames = [];
    
    zooAnimals.forEach(function animalNames(element){
        
    var display = "name: " + element.animal_name + ", " + "scientific: " + element.scientific_name
       
     displayNames.push(display);
         
      })
      console.log(displayNames); 

again i get the correct array back and the data looks correct...but animalNames comes back as undefined...i cannot remove this portion i am to keep it there but i do not know what to do with it.

code/question and array

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I believe you are following this challenge. In this case, make sure to read the instructions properly:

The zoos want to display both the scientific name and the animal name in front of the habitats. Use animalNames to populate and return the displayNames array with only the animal name and scientific name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "name: {name}, scientific: {scientific name}"

So from the instructions you are expected to create a function animalNames() that creates an array named displayNames and returns this array from within your function animalNames(). The array displayNames contains strings of the sturcture name: animal_name, scientific: scientific_name.

// you could set zooAnimals as a required parameter in your function 
// but as it is defined in the same file you can access it directly as well
function animalNames() {
    const displayNames = [];
    // you have to do the forEach on zooAnimals here
    zooAnimals.forEach(animal => displayNames.push(`name: ${animal.animal_name}, scientific: ${animal.scientific_name}`))
    // return an array
    return displayNames;
}

The way you did it, the function animalsName() is an anonymous function that only exists within the zooAnimals.forEach() call. Therefore, it is undefined.

about 4 years ago · Juan Pablo Isaza Relatório

0

try this, it defines a function animalNames as separate function

    const displayNames = [];
    const zooAnimals = [
     {animal_name:"Jackel",scientific_name:"Canine"}
    ]
    function animalNames({animal_name, scientific_name}){
      return `name: ${animal_name}, scientific: ${scientific_name}`
    }
    zooAnimals.forEach((element)=>{
     displayNames.push(animalNames(element));
    })
    console.log(displayNames); 

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