Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Functions Return Values

I'm confused. Please help!

Challenge Question: Around line 17, create a function named 'arrayCounter' that takes in a parameter which is an array. The function must return the length of an array passed in or 0 if a 'string', 'number' or 'undefined' value is passed in.

I wrote this function:

  ```function arrayCounter (array) {
    if (typeof array === 'number', 'string', 'undefined') {
        return 0;
    }
    return array.length;
  }```

I'm getting back "Bummer! If I pass in an array in to 'arrayCounter' of '1,2,3' I get 0 and not the number 3."

I don't understand why it is inputting what looks like a string '1,2,3' or a set of numbers 1,2,3 when they specify to return 0 if it's a string or numbers. I went back into the lessons to try to find what I'm missing and I can't find anywhere that the way to input an array into an argument is '1,2,3'.

What's wrong with my function? And why is it inputting '1,2,3' as an argument that is supposed to be an array?

Thanks in advance.

2 Answers

Adam Moore
Adam Moore
21,956 Points

It appears you have to set a separate "typeof" test for each, like:

function arrayCounter(array){
        if(typeof array === "string" || typeof array === "number" || typeof array === "undefined"){
          return 0;
        }
          return array.length;
      }

Thanks Adam. That worked.