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 trialAdam Duffield
30,494 PointsJavascript Functions
Hey, Here is the challenge set...
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.
and here is my code....
function arrayCounter (
var my_array = [];
)
{if(my_array === 0){console.log("Fuck yeah!")};
}
Any ideas where I'm going wrong with this?
Thanks,
3 Answers
dan schmidt
2,576 PointsYou need to call the length
property on the array:
[1,2,3].length === 3 ? 'yep' : 'nope';
Edit:
Also, you cannot declare variables within your function's parameters.
function arrayCounter(myArray) {
if (myArray.length === 0) { console.log('Fuck yea'); }
}
arrayCounter([]);
dan schmidt
2,576 PointsAlso understand that since we are not using return statements in our function so far, it will implicitly return 'undefined'
Ruben Leija
4,051 Points"The function must return the length of an array passed in or 0 if a 'string', 'number' or 'undefined' value is passed in."
This question doesn't make much sense to me. A value will either always be a string, number or just null. So no matter what values is in your array the function will always return 0.