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

Return 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
anthony de alwis
4,146 Points
function arrayCounter(array){

        if(typeof array==='string'){
        return;
        }
        if(typeof array==='number'){
        return;
        }
        if(typeof array==='undefined'){
        return;
        }
    else{ return array.length;}
       };

This works! I also got stuck with this code challenge. Thanks Anthony for your help.

You need to change the return to return 0. Or else it will just return undefined.

Along 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; };

Stone Preston
Stone Preston
42,016 Points

you 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
Jaclyn Tsui
4,834 Points

Hi Stone, could you elaborate?

I'm getting an error with Anthony's code.

thanks anthony, that works.

Juan Pablo Vasquez
Juan Pablo Vasquez
3,521 Points

missing the "else" statement... thank!