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 trialChristoffer Boye-Hansen
3,810 PointsFunction that takes an Array as paramater
Hi.
I am on a Javascript challenges that says: 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 know how to create a function, but how can I make it take an Array as a parameter?
2 Answers
Mike Staebell
6,177 PointsI had the same question. The wording of the question is a little misleading; you don't need to do anything other than separate your arguments with commas.
function myExample(these, are, your, arguments, AKAparameters, and, technically, are, an, array) {
//code here
}
myExample (these, values, will, define, the, parameters, above);
By setting multiple arguments, you're essentially declaring that the parameter is an array.
I hope that helps.
Robert Ho
Courses Plus Student 11,383 PointsHey Christoffer,
Javascript function definitions don't expect parameters to be a specific type and when executed they don't do any type checking on the arguments you pass in. Basically what that means is if you do something like
function arrayCounter(blah){
/* code /*
}
The function is never going to check what the parameter type for blah is. Therefore, any type of value (strings, numbers, undefined, null, even functions!) can be passed into the arrayCounter function as the blah parameter!. Its up to your code to check what the types are. I watched this video a long time ago so I don't remember what they explain exactly, but a hint I would give you is to use the Array.isArray(blah) method in your code when you are checking the parameter type. Good luck!