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 trialAlex Gustafson
14,207 PointsCode Challenge help: Creating the javascript function arrayCounter
I'm working on this challenge: "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."
Here was my attempt:
function arrayCounter (someArray){
var arrayCount = 0;
if (typeof someArray === 'string', 'number', 'undefined')
{
return arrayCount;
}
arrayCount = someArray.length;
return arrayCount;
}
The code challenge is telling me that when it passes an array '1,2,3' it gets the result 0 not 3. I'm not sure where my error is though? Am I using the function return incorrectly? Something wrong with the scope of the argument someArray or the variable arrayCount? Is the typeof operator not behaving as I thought?
1 Answer
Phillip Gibson
13,805 PointsYour if
block is formatted incorrectly. What you are probably looking for is the ||
(or) operator, which will allow you to define multiple conditionals within that same if
check.
if (typeof someArray === 'string' || typeof someArray === 'number' || typeof someArray === 'undefined') {
return arrayCount;
}
Alex Gustafson
14,207 PointsAlex Gustafson
14,207 PointsThis worked! Thanks!
Phillip Gibson
13,805 PointsPhillip Gibson
13,805 PointsGlad to be of assistance. :)