Hello!
The question I have is in regards to a question from another project that is not on Codeacademy so I do not have a link but I will post the problem below:
" Define a function, zooInventory
, that accepts a multi-dimensional array of animal facts.
zooInventory
should return a new, flat array. 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]]
];
zooInventory(myZoo);
/* => ['King Kong the gorilla is 42.',
'Nemo the fish is 5.'
'Punxsutawney Phil the groundhog is 11.']
*/
I am able to loop through nested arrays and flatten them into one array, but the part that is giving me trouble is that it wants me to add the string 'is ’ before the age of the animal. I wasn’t sure what the best method to use to distinguish when the array contains a number and to add that string beforehand. I added a part that I felt would identify that by seeing if the array loosely equaled a number. That isn’t causing an issue but also isn’t working correctly. Here is what I have so far:
function zooInventory(arr) {
let zaboomafoo = [];
for (let i = 0; i < arr.length; i++) {
let element = arr[i];
if (Array.isArray(element)) {
for (let j = 0; j < element.length; j++) {
let name = element[j];
if (element[j] || element[i] == Number) {
"is " + element[j] || element[i];
}
zaboomafoo.push(element);
}
} else {
zaboomafoo.push(element);
}
}
return zaboomafoo;
}