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!
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

Joe Thompson
4,777 PointsReturn Values Challenge Problems
Cannot figure out the JavaScript Foundations Return Values Code Challenge. Here is what I have:
var my_array = [1, 2, 3];
function arrayCounter (my_array) {
if (typeof arrayCounter === "string") {
return 0;
}
if (typeof arrayCounter === "number") {
return 0;
}
if (typeof arrayCounter === "undefined") {
return 0;
}
return arrayCounter.length;
};
When I check my work it comes back with "Bummer! Wrong value returned from 'arrayCounter(undefined)' it should be 0". Yet, I have a 0 as a return after undefined? What am I doing wrong?
4 Answers

anthony de alwis
4,146 Pointsfunction arrayCounter(array){
if(typeof array==='string'){
return;
}
if(typeof array==='number'){
return;
}
if(typeof array==='undefined'){
return;
}
else{ return array.length;}
};

Stone Preston
42,016 Pointsyou have the name of the function as the variable, (arrayCounter) which is incorrect. it should be the parameter (my_array)
instead of
typeof arrayCounter == blah
you should have
typeof my_array == blah blah blah

Jaclyn Tsui
4,834 PointsHi Stone, could you elaborate?
I'm getting an error with Anthony's code.

Shankar Narayan
9,099 Pointsthanks anthony, that works.

Juan Pablo Vasquez
3,521 Pointsmissing the "else" statement... thank!
Cliff Garibay
7,023 PointsCliff Garibay
7,023 PointsThis works! I also got stuck with this code challenge. Thanks Anthony for your help.
Brandon Harvey
14,672 PointsBrandon Harvey
14,672 PointsYou need to change the return to return 0. Or else it will just return undefined.
Gina Sabori
8,664 PointsGina Sabori
8,664 PointsAlong with return 0, remove the else{ .. .} this is what finally worked for me.
function arrayCounter(array){ if (typeof array === 'string'){ return 0; }; if (typeof array === 'number'){ return 0; }; if (typeof array === 'undefined'){ return 0; }; return array.length; };