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

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,622 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,622 Points

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