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 trialJacob Miranda
19,392 PointsJS Foundation - Return Values
I've tried everything! If anyone can explain this to me, I'm not understanding too well what it's asking for
The challenge says:
"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."
Edit: I can't get the Anonymous Function code challenge to work either -.-
6 Answers
Joe Thompson
4,777 PointsJacob, I cannot figure this one out either. However, I was able to figure out the anonymous functions challenge.
part 1:
place the anonymous function after the 'anonymousFunction' variable
part 2:
place the () after the 2nd function to execute it
Stone Preston
42,016 PointsHere is a psuedocode version of the function that should help
function arrayCounter ( array)
if (array is of type 'array')
return length of array
else
return 0
anthony de alwis
4,146 Pointsfunction arrayCounter(array){
if(typeof array==='string'){
return 0;
}
if(typeof array==='number'){
return 0;
}
if(typeof array==='undefined'){
return 0;
}
else{ return array.length;}
};
Jacob Miranda
19,392 PointsWhy is what I put wrong?
function arrayCounter(array) {
}
if (typeof array === 'string') {
return 0;
}
if (typeof array = 'number') {
return 0;
}
if (typeof array = 'undefined') {
return 0;
}
else { return array.length; }
}
Jacob Miranda
19,392 Pointsactually that's not what I put. Woops, wrong one. But I got it to work. Thanks guys.
Andrew Chappell
12,782 PointsSo you have to create seperate if statements for 'number', 'string' and 'undefined' and set them all to return 0; instead of just return;
Here is the code:
function arrayCounter (array) {
if (typeof array === 'string') {
return 0;
}
if (typeof array === 'number') {
return 0;
}
if (typeof array === 'undefined') {
return 0;
}
return array.length;
}
anthony de alwis
4,146 Pointsdon t forget else { return array.length;}
Chase Lee
29,275 PointsHere is a thread on how to display code in the forum.
Jacob Miranda
19,392 PointsOh I know how to, I just didn't put my code cause I was way off.
John Shofstall
14,308 Pointsfunction arrayCounter(array) {
if (type of array === 'string' | 'number' | 'undefined') {
return 0;
}
return array.length;
}
why is that wrong? i can't simplify into one if statement with ors? "|" is the character for "or" right?
Joe Thompson
4,777 PointsJohn, the or operator is || not |.
Hope this helps.
John Shofstall
14,308 Pointsthank you, that will prevent future errors. but that still didn't work, i guess we have to write separate statements for now.
anthony de alwis
4,146 Pointsanthony de alwis
4,146 Pointshere is the answer dude: