Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Foundations Functions Return Values

Jahan Zeb
Jahan Zeb
11,169 Points

Can't pass JS code challenge

Hey guys, please help me out passing this code challenge:

http://teamtreehouse.com/library/return-values

This is the code I am using:

function arrayCounter(anArray) {
        if (typeof anArray === "undefined") {
                return 0;
        }
        else if (anArray[0] instanceof anArray){
                return anArray.length;
        }
        else {
                return 0;
        }
}

2 Answers

You are doing a few things wrong here. First you can make this challenge pass with just an if/else.

The logic is: if the variable type is an array, return the length of the array, else return 0.

When you're checking your instance of an array, you are asking if the first array value is equal to its parent array. You need to check if it is an instance of an "Array".

I wont give you the full answer, but I will get you close enough for you to be able to find your way there:

     function arrayCounter (arr){
        if(/* the arr var is an instance of an Array */){
          return /* the length of the array */;
        } else {
            return 0;
        }
      }
Jahan Zeb
Jahan Zeb
11,169 Points

Thank a bunch Riley for the hint and making me do it instead! Now I know where I was wrong.

function arrayCounter (anArray) {
        if (anArray instanceof Array) {
                return anArray.length;
        }
        else {
            return 0;
        }
}

Cheers!