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

Hunter Berge
Hunter Berge
5,065 Points

How to return an array within a function

"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 am on Stage 5 of the Java Basics course. They are asking to return an array in a function. I cannot figure out how to get it done. Here is what I have:

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

Please help. These challenges are really starting to frustrate me...they give really confusing instructions then do not tell you what you are doing wrong when you cannot figure it out. And I feel like I shouldn't have to 'Ask a Question' every time I cannot figure something out...

Thanks

2 Answers

Hugo Paz
Hugo Paz
15,622 Points

The way you are thinking is correct but the syntax is wrong.

function arrayCounter(a){
        if(typeof a === "string" || typeof a === "number" || typeof a === "undefined"){
        return 0;
        }
        else{ 
          return a.length;
        }
      }

Remember to use length method on the else

Chris Shaw
Chris Shaw
26,676 Points

Hi Hunter,

There are plenty of other threads on the forum about this and they have a lot of information especially about this topic.

https://teamtreehouse.com/forum/code-challenge:317