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


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

/* => ['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];
    } else {
  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

What promises are you given about your input?
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