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 trialLouis Darby
5,149 PointsReturn 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
banu babanu
8,152 PointsTry this:
function arrayCounter(array) {
if(Array.isArray(array)) {
return array.length;
}
else { return 0;}
}
Shawn Anderton
24,124 Pointscan you post the whole function.
Louis Darby
5,149 PointsThe 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.
banu babanu
8,152 PointsYou must use Array.isArray() method.
Louis Darby
5,149 PointsLouis Darby
5,149 PointsPerfect, thanks a lot!