# How to distinguish a number in an array and add a string

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;
}``````

I apologize if there is a guideline against posting questions from other sites, I will delete this right away if it can only be from a codeacademy question

Itâ€™s not just â€śisâ€ť in front of one thing, itâ€™s â€śtheâ€ť in front of another as well. You canâ€™t use the type to determine whether that should happen because this is not the case for all strings.

So give some thought about how youâ€™re supposed to determine whatâ€™s what. Look at an entry. What do you do to say what goes where?

1 Like

Imagine thereâ€™s a million other entries that you need to handle as well.

You canâ€™t write code that only applies to the input youâ€™ve seen. You have to make use of how it is promised that they will look.

Btw, I doubt the array has Number in it, so you probably shouldnâ€™t be testing whether an array element is equal to Number, and your use of `||` is probably super wrong, maybe you meant to compare two things to Number? Then you would need two uses of `==` â€¦right? Well itâ€™s not something thatâ€™s useful to do anyway for at least the two reasons Iâ€™ve mentioned.

Presumably the task exists in some kind of context which would say a lot about what operations youâ€™re expected to use.
Consider picking them out directly with indexing or destructuring unless thereâ€™s something saying that the structure changes in any way.

1 Like