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

Functions: Return Values Challenge

Challenge is: 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 have tried these but keep getting an SyntaxError - Parse Error. I'm confused by how to format the argument or if I was supposed to define an array variable first? I'm missing something obviously and I'm stuck here.


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

and also this:

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

1 Answer

Hi Susan,

You're pretty close. You have to check the type against each string separately rather than join them all with the comma operator. Also you want to return the length of the array at the end without the equal sign return array.length; Same thing with return 0; No equal sign.

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

        return array.length;
}

Each type is checked separately. There are better ways to solve this problem but that is about what can be expected of you at this point.

That was the next thing I was wondering if we had to do several if statements. Dang, if only there was a shorter way.. :)

thank you, glad I was at least on the right track!

With the logical OR operator, ||, you can combine the if statements and remove that duplication. I don't think this has been introduced yet.

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

        return array.length;
}

You can read that as "if the type is undefined OR the type is string OR the type is number then return 0"

Probably the best way is to use the Array.isArray() method.

function arrayCounter (array){
        if(Array.isArray(array)) {
          // It's an array so return the length
          return array.length;
        }

        // It must not have been an array so return 0
        return 0;
}

You're welcome.

Yes, that 1st solution is the long way but as you learn more you'll be able to do it in shorter and more robust ways.

That all makes sense! Thank you for taking the time - I am just so glad CodeOregon is providing this opportunity. I'm really looking forward to using this hopefully in a new career someday....

Best of luck to you!

This is a slightly shorter, more direct way to condition the return value:

function arrayCounter (array){
        if(typeof array === 'object') {
             return array.length;
        }

        return 0;
}

Hi Erik,

I could be wrong but I don't think you would know at this point that the typeof operator will return 'object' for an array. Also, while this is shorter than what I think is the expected solution it still suffers from similar problems.

An object or null could be passed in and these will also make the if condition true. Then you'll either be returning undefined in the case of the object or generate a typerror with null because null has no properties.

The original solution I posted also suffers from these same types of problems. It does fail on more things than your solution though.

The isArray method is also short and doesn't suffer from these problems.

Thanks Jason, I didn't know until now that a value of null would pass that condition :P