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

Louis Darby
Louis Darby
5,149 Points

Return Value Challenge

For returning 0 if a number was input into the function I wrote:

if (typeof a === 1) { return 0; } However it returns undefined, why is this? And how can I get it to return 0.

3 Answers

Try this:

function arrayCounter(array) {

 if(Array.isArray(array)) {
               return array.length;
      }
      else { return 0;}

      }
Louis Darby
Louis Darby
5,149 Points

Perfect, thanks a lot!

can you post the whole function.

Louis Darby
Louis Darby
5,149 Points

The question is:

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 wrote:

function arrayCounter (a) { if (typeof a === 'undefined') { return 0; } if (typeof a === "string") { return 0; } if (typeof a === 1) { return 0; } };

but the last part seems to not work.

You must use Array.isArray() method.