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

sven molhuijsen
sven molhuijsen
7,091 Points

problem with js arrayCounter function

in this codechallenge i have to make a function, 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.

 function arrayCounter(x){

        if(typeof x=="array"){

          return x.length;

        }else if

          (typeof x=="number" || typeof x=="string" || typeof x=="undefined"){

            return 0;

          }

      }

But when i submit this code I’ve got this error:

Bummer! If I pass in an array in to 'arrayCounter' of '1,2,3' I get undefined and not the number 3.

But I can’t find syntax errors and when I watch other examples I still cant find a solution.

Maybe you can help me by finding the issue? (sorry for my English, I come from The Netherlands, so it’s not perfect)

3 Answers

Janek Rezner
Janek Rezner
12,973 Points
function arrayCounter (x){
    if (typeof x === "undefined" || typeof x === "string" || typeof x === "number"){
      return 0;
    }
    return x.length;
  }

ignore me..