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

Terence Chan
Terence Chan
11,867 Points

Not quite sure what to do for this code challenge? Could anyone provide a hint or direction please? Thanks

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.

2 Answers

Hugo Paz
Hugo Paz
15,620 Points

First you create a function that receives a parameter, lets say function myFunction(parameter)

Then inside, you need to check if parameter is a string, a number or undefined, if its one of these, you return 0. Else you return the length of the parameter.

I remember this one, because I had (probably the same) problem understanding it. Not on what to do, but how to phrase it.

You either had to do several "if" and "else if" statements testing if it's a "typeof" a string, a number, or undefined, or do it on one line by using the pipe symbol as an "or", so on one line you're testing if it's a "typeof" a string OR a number OR undefined.

Terence Chan
Terence Chan
11,867 Points

Thanks Hugo and Sau Ting.
How do I check if a parameter is a number or not? I think I got the "string" or "undefined" bit, but not the number one. And what is the symbol for "or"? Many thanks

Hugo Paz
Hugo Paz
15,620 Points

Same as the other ones, typeof varname === "number". For "or" you can use ||