Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda