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 trialMarcus Sacco
2,153 PointsTesting an array with an if/else
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.
So, I created a function named arrayCounter. It needs to take a parameter which is an array. I'm assuming I need a name for this parameter, so I named it myArray. I then created an if/else statement to test if the parameter passed is an array. If it is, the function returns myArray.length. Else, it returns the value 0. This seems to be in line with the instructions, but it always returns 0, instead of 3 after being passed an array of 1,2,3.
```function arrayCounter(myArray){ if(typeof myArray === 'array'){ return(myArray.length); }else{ return(0); } }
1 Answer
y0mki
14,218 PointsHi Marcus,
I just found out this question has been already answered before on the forums, so here's the link to it. Hope it helps :-)