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

Hossam Khalifa
Hossam Khalifa
17,200 Points

I CAN NOT SOLVE THIS

function arrayCounter(name){
    if(typeof name == "undefined","string","number"){
        return 0 ;

    }
else if(typeof name =='array'){
        return name.length;
}

    }

6 Answers

Using commas in if conditions will result in being syntax errors.

Instead, you need to use the or operator. You can find more info about the or operator right here.

if( typeof name == 'undefined' || typeof name == 'string' || typeof name == 'number') {

}

In addition to this, the else if should just be an else statement. The typeof operator returns "object", not "array" for arrays.

Elizabeth Frazier
Elizabeth Frazier
10,187 Points

I think you're missing some brackets.

Elizabeth Frazier
Elizabeth Frazier
10,187 Points

I think you're missing some brackets.

Dino Paškvan while he could switch to else over elseif that could be bad. Because the way it stands now he's specifying "else if" typeof name == array. therefore if he switched to else all other typeof names that are not handled by the first if clause would be recognized if switching to else. It's better to be non inclusive than all inclusive in my opinion. The onlytime I use "else" is if I am comparing a boolean value where the state can only be true or false.

Code challenges on Treehouse usually require you to repeat the code from the video, or to do something similar to what was achieved in the video.

The code in the Return Values video is structured in this manner:

function someFunction(someParameter) {
   if (typeof someParameter === "undefined") {
      return 0;
   }
   return someParameter.length;
}

So the analogous solution would be just to expand on the if condition and add types that the function is supposed to return 0 for (undefined, string and number).

But that wasn't my point.

The function is not concerned with other types (objects, functions, booleans...). And there's a reason for that. The video introduces the typeof operator. That particular operator cannot detect arrays. It returns "object" for them, the same it does for objects and null.

My point was that if the code was left like this:

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

It wouldn't work for arrays. It would return 0 for undefined, strings and numbers, but for arrays (and any other types) it would return undefined because the else if condition will never evaluate to true — the typeof operator will never return "array".

I agree that it's always good to filter out unexpected behaviours, but that's beyond the scope of that video.

You cannot make foolproof array detection with the typeof operator. For that you'd have to use Array.isArray(), or if you're in an ES3 environment Object.prototype.toString.call() and check the return value against the string "[object Array]". But again, that's something that's outside of the scope of that particular lesson.

Hossam Khalifa
Hossam Khalifa
17,200 Points

Thank you Mr.Dino Paškvan Your the best one in treehouse.