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

javascript foundation functions - missing something?

I tried return "0"; and still got error - any help! thanks

function arrayCounter (arrayIn) { if (typeof arrayIn === 'undefined') { return 0; } else{ return arrayIn.length; } }

  arrayIn = ["A"];
  arrayCounter(arrayIn);

3 Answers

Adam Moore
Adam Moore
21,956 Points

Here is a good explanation of how to tackle this challenge and what the challenge is looking for: Function and Length

Hi Philip,

You've only checked if the type is 'undefined' but you need 2 more if's to check if the type is a 'string' or a 'number'

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

Thanks!