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 trialKevin Emehizer
1,826 PointsCannot figure this out
I have this as my function for this challenge
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.
function arrayCounter(array) { if(typeof array === 'undefined', 'string', 'number') { return 0; } return array.length; }
but get this error: If I pass in an array in to 'arrayCounter' of '1,2,3' I get 0 and not the number 3.
not sure why
3 Answers
Richard Duncan
5,568 PointsYou're almost there but you have got confused with operators and arguments in a function name. The if statement will evaluate a condition, to achieve what you are trying to do there you need to use the or operator || and offer an alternative with the else statement.
For example : -
function arrayCounter(array) {
if ( typeof array === 'undefined' || typeof array === 'NaN' ) {
return 0;
} else {
return array.length;
}
}
Kevin Murphy
24,380 PointsHey Kevin, this particular challenge has been addressed and explained nicely elsewhere in the forum. You need to check each of the values of the array individually. This can be done in sequence or by using the "OR" operator which would keep the code DRY. Circle back if you can't find it.
Kevin Emehizer
1,826 PointsThanks guys. I ended figuring it out just now. Really appreciate the quick responses