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

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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