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

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!