Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Foundations Functions Return Values

Keep getting "Wrong value returned from 'arrayCounter(undefined)' it should be 0" but I dont understand why.

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

4 Answers

First of all, you can't list multiple comparisons in an if statement like that. You combine them like this:

if (typeof array === 'string' || typeof array === 'number' || typeof array === 'undefined') {
   // the rest of your code
}

You are also not returning a value inside the if statement. By setting the value of array to 0, you've switched the type of that value to Number. Numbers don't have the length property, so even if you returned array.length, you'd be returning undefined.

Instead, just return a 0 in the if statement.

Thank you Dino!

That cleared up a lot of confusion.

Jay Holt
Jay Holt
15,617 Points

I was having trouble with this one too. Thanks for this.

Shane Meikle
Shane Meikle
13,188 Points

The question appears to be stating that anything other than an array should return a 0, but in your code, you are setting array to = 0?

Basically, I think you should be looking to return the other values as 0 and if none of the other values exist, return the length of the array.

"The question appears to be stating that anything other than an array should return a 0, but in your code, you are setting array to = 0?"

I thought I was setting array = 0 inside the 'if statement' So yes, I am setting it to 0 if the array is a 'string' 'number' or 'undefined'

Basically, I think you should be looking to return the other values as 0 and if none of the other values exist, return the length of the array.

How do I return the "other values as 0" ?

Jay Holt
Jay Holt
15,617 Points

Instead of saying "array = 0;" could you just say "return 0;" ?

You got it Jay!

Return 0; was the change that was need to be made. Thanks for the support