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

Robert Mills
Robert Mills
16,243 Points

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.

Why is this telling me I am not checking if an undefined variable is being passed in? i have cleary checked for strings, numbers and undefined variables with the if statements.

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

  }

3 Answers

Hi Rober- Maybe it's because you don't have and else statement. Please see below! Hope this helps.

function arrayCounter(array) {
        if(typeof  array === 'undefined') {
          return 0;
        }
        if(typeof array === 'number') {
          return 0;
        }
        if(typeof array === 'string') {
          return 0;
        }
        else {
          return array.length;
        }
      }
Timothée Goguely
Timothée Goguely
11,147 Points

Hi Robert and Denisse! As you may know, developers are lazy. So you can even be more efficient by using the OR operator || as below:

function arrayCounter(array) {
  if (typeof  array === 'string' || typeof array === 'number' || typeof array === 'undefined') {
    return 0;
  }
  else {
    return array.length;
  }
}
John Grillo
John Grillo
30,241 Points

Damn this is amazing. Ive done hard-core C++ programming and hadn't quite figured this out yet; the javascript section seems to bounce around from simple to frutratingly difficult. This challenge seems to have gone up to 11 here and it's confusing given the simplicity of some the others. I think the adding of hints to things like this would be really helpful.

It could have specifically said 'check it against these random words we use. Hey, does 'string' mean a thing of the string type or the number-type when you use 'number' like that?

And for anyone else asking, YES, this answer works. goguely++!

itsports
itsports
4,060 Points

I don't like how it's asking you to pass a parameter as an array, but absolutely no mention, or examples of arrays being passed as parameters are mentioned in the video. That made this challenge hard, and... not fair.