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

170
Vistas
JavaScript function that takes a multidimensional array, flattens it, then returns array values as string in choice order

Having trouble with a certain objective where I have to create a function that takes a multidimensional array and returns a flat array with sentence string values using values from the given multidimensional array. I'm having a hard time iterating through the array and getting it to push the values to a new array. Everything I've tried returns the values in the wrong spots and now it just returns undefined. I'm so lost and frustrated

Define a function, zooInventory, that accepts a multi-dimensional array of animal facts. zooInventory should return a new, flat array of strings. Each element in the new array should be a sentence about each of the animals in the zoo.

let myZoo = [
  ['King Kong', ['gorilla', 42]],
  ['Nemo', ['fish', 5]],
  ['Punxsutawney Phil', ['groundhog', 11]]
];

function zooInventory(zooList) {
  let zooFlat = [];
  let name = [];
  let animal = [];
  let age = [];
  for (let i = 0; i < zooList.length; i++) {
    if (!Array.isArray(zooList[i])) {
      name.push(zooList[i])
    } else {
      animal.push(zooList[i][0]);
      age.push(zooList[i][-1]);
    }
  }
  for (let j = 0; j < name.length; j++) {
    zooFlat.push(`${name[j]} the ${animal[j]} is ${age[j]}.`)
  }
  return zooFlat;
}
zooInventory(myZoo);
/* => ['King Kong the gorilla is 42.',
       'Nemo the fish is 5.'
       'Punxsutawney Phil the groundhog is 11.']
*/
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Following up on my comment:

TS Playground

const myZoo = [
  ['King Kong', ['gorilla', 42]],
  ['Nemo', ['fish', 5]],
  ['Punxsutawney Phil', ['groundhog', 11]],
];

function createSentence (item) {
  const [name, animal, age] = item.flat();
  return `${name} the ${animal} is ${age}.`;
}

function zooInventory (zooList) {
  return zooList.map(createSentence);
}

console.log(zooInventory(myZoo));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your assignment is very specific, so I would go for a very specific solution:

let myZoo = [
  ['King Kong', ['gorilla', 42]],
  ['Nemo', ['fish', 5]],
  ['Punxsutawney Phil', ['groundhog', 11]]
];

function zooInventory(zooList) {
  return zooList.map(animal => animal[0] +' the '+ animal[1][0] +' is '+ animal[1][1] +'.')
}

console.log(zooInventory(myZoo))

As you can see, you don't need to do anything like flattening if your assignment is exactly this, because you can just concatenate the string reading from nested arrays.

about 4 years ago · Juan Pablo Isaza Denunciar

0

... techniques used ...

  • Array.prototype.map

  • Array.prototype.flat

  • Destructuring Assignment / Array Destructuring

  • Template Literals

console.log([

  ['King Kong', ['gorilla', 42]],
  ['Nemo', ['fish', 5]],
  ['Punxsutawney Phil', ['groundhog', 11]]

].map(arr => {
  const [name, animal, age] = arr.flat();
  return `${ name } the ${ animal } is ${ age }`;
}));
.as-console-wrapper { min-height: 100%!important; top: 0; }

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