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 trialJoshua Peters
1,739 PointsKeep getting "Wrong value returned from 'arrayCounter(undefined)' it should be 0" but I dont understand why.
function arrayCounter(array) {
if (typeof array === 'string', 'number', 'undefined') {
array = 0;
};
return array.length;
}
4 Answers
Dino Paškvan
Courses Plus Student 44,108 PointsFirst of all, you can't list multiple comparisons in an if
statement like that. You combine them like this:
if (typeof array === 'string' || typeof array === 'number' || typeof array === 'undefined') {
// the rest of your code
}
You are also not returning a value inside the if
statement. By setting the value of array
to 0, you've switched the type of that value to Number. Numbers don't have the length
property, so even if you returned array.length
, you'd be returning undefined
.
Instead, just return a 0
in the if
statement.
Shane Meikle
13,188 PointsThe question appears to be stating that anything other than an array should return a 0, but in your code, you are setting array to = 0?
Basically, I think you should be looking to return the other values as 0 and if none of the other values exist, return the length of the array.
Joshua Peters
1,739 Points"The question appears to be stating that anything other than an array should return a 0, but in your code, you are setting array to = 0?"
I thought I was setting array = 0 inside the 'if statement' So yes, I am setting it to 0 if the array is a 'string' 'number' or 'undefined'
Basically, I think you should be looking to return the other values as 0 and if none of the other values exist, return the length of the array.
How do I return the "other values as 0" ?
Jay Holt
15,617 PointsInstead of saying "array = 0;" could you just say "return 0;" ?
Joshua Peters
1,739 PointsYou got it Jay!
Return 0; was the change that was need to be made. Thanks for the support
Joshua Peters
1,739 PointsJoshua Peters
1,739 PointsThank you Dino!
That cleared up a lot of confusion.
Jay Holt
15,617 PointsJay Holt
15,617 PointsI was having trouble with this one too. Thanks for this.