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 trialMuhammad Ali Khan
8,227 PointsChecking if 'Undefined'
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.
var my_arrray = [ 1 , 2 , 3 ];
function arrayCounter(my_array){
if (my_array.isArray) {
var a = my_array.length
}
else {
a = 0
}
return a
}
Bummer! You're not checking if 'undefined' is being passed in
Please help
5 Answers
Carla Thomas
Front End Web Development Techdegree Student 16,039 PointsI found the "answer" here in the forum...it makes sense but I cannot get it to work in the challenge. Here's the link to the answer....please let me know if you can make that code work
https://teamtreehouse.com/forum/this-is-driving-me-crazy-what-am-i-missing
The answer
function arrayCounter(array)
{ if (typeof array === "string" || typeof array === "number"|| typeof array === "undefined")
{ return 0; }
else { return array.length; } }
James Andrews
7,245 Pointsif(typeof my_array === 'undefined'){
// handle error
}
else
{
// your code here etc...
}
James Andrews
7,245 Points...
Muhammad Ali Khan
8,227 PointsCarla Thomas i got it right the first time as well but now when i try it, it keeps on giving me syntax error...strange
Adama Sy
7,076 Pointsfunction arrayCounter (array) { if (typeof array === "string"){ return 0; } if (typeof array === "undefined"){ return 0; } if (typeof array === "number"){ return 0; }
else {return array.length};
}
James Andrews
7,245 PointsJames Andrews
7,245 PointsProgrammatically this code works. Why it doesn't work in the challenge I couldn't say as I haven't taken the javascript track yet