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
Ashley Omnas
5,881 PointsNeed Help W/ Javascript Activity
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. I am really having trouble understanding how to do this activity. So... I can start the function either by declaring var arrayCounter = function (array) { }; or function arrayCounter (array){ }; then within the curly brackets I would set up an if statement that tests if the value passed in is a string, number, or undefined... I'm not sure how to test for that though. I believe to test for undefined I would do if(typeof array === 'undefined'). After the if statement, I believe I would simply do return array.length; Tips, suggestions, and pointers would be much appreciated.
1 Answer
Jordan Morphew
9,750 PointsHi Ashley,
So the you are not incorrect with either of those ways for defining a function. The way is was shown in the video before that task is function arrayCounter(array){} so you may want to stick with that.
You are also on the right direction with you for the if statement as well. Your check for the undefined is completely correct. If you look at this link typeof documentation it shows you types that will be returned by typeof so if you modify your correct if statement that looks for undefined to look for the other outputs you can set it to return 0 when it equates to that.
You are correct for the return array.length; aswell that will work.
You have all the correct thoughts it is just a matter of putting them all together.