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 trialamouhlohbeng
Courses Plus Student 3,296 PointsHow to check if an array value is a number
Hi all, can someone help me with checking if a value in an array is a number. I am stuck with this challenge. Thanks http://teamtreehouse.com/library/return-values
4 Answers
Andrew Shook
31,709 PointsSo what the challenge is looking for is a function that checks to see if the parameter passed into the function is an array. So what you need to do is use the "typeof" method to check to see if the parameter is a string, number or undefined. If the parameter is one of those you need to return 0, and if it's not then you need to return the length of the array. Try this code:
var arrayCounter = function(x){
if(typeof(x) == "object"){
return x.length;
}else{
return 0;
}
}
If you notice, when I used the typeof method I didn't use "array". This is because in javascript, typeof(array) will return "object" not "array". So when checking to see if something is an array you need to compare it to the string "object". It's just one of those weird things about the language. Hope this helps.
Askia Harris
7,962 Pointsi'am so stuck on this i dont know what im doing wrong
Andrew Shook
31,709 PointsCan you post your code? That might help determine what your problem is.
Askia Harris
7,962 Pointsfunction(arrayCounter){ if (typeof arrayCounter === "string") { return 0;} else if(typeof arrayCounter === "name"){ return 0; } else if(typeof arrayCounter === "undefined"){ return 0; } else {return arrayCounter.length;}
}
Askia Harris
7,962 Pointsfunction(arrayCounter){ if (typeof arrayCounter === "string") { return 0;} else if(typeof arrayCounter === "name"){ return 0; } else if(typeof arrayCounter === "undefined"){ return 0; } else {return arrayCounter.length;}
}
Askia Harris
7,962 Pointsfunction(arrayCounter){ if (typeof arrayCounter === "string") { return 0;} else if(typeof arrayCounter === "name"){ return 0; } else if(typeof arrayCounter === "undefined"){ return 0; } else {return arrayCounter.length;}
}
Andrew Shook
31,709 PointsYou have a repeated syntax error with the typeof function. You need to put parentheses around arrayCounter like so; typeof(arrayCounter).
Askia Harris
7,962 PointsThanks Andrew but i'am still coming up with a error
Andrew Shook
31,709 PointsReplace name with number.
amouhlohbeng
Courses Plus Student 3,296 Pointsamouhlohbeng
Courses Plus Student 3,296 PointsThanks Andrew, I have not yet succeeded; maybe I am not implementing your suggestion well. I'll let you know how things go. Amo
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsI just double checked my code in the challenge and it passed. Let me know if you are still having problems
amouhlohbeng
Courses Plus Student 3,296 Pointsamouhlohbeng
Courses Plus Student 3,296 PointsHello Andrew, your code did go through. I am not quite sure what I was doing wrong. After several tries success!!! Thanks a lot. Amouh