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 trial
Gabriel Brewdant
783 PointsCode challenge: JavaScript Foundations -- Functions -- Return Values.
I'm stuck on a code challenge: JavaScript Foundations -- Functions -- Return Values.
While I'm not confident that my code is correct, I think the error message I'm receiving is incorrect. Will you please help?
The directions are:
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.
My code is:
function arrayCounter(par){
if (typeof par ===’undefined’){return 0;}
if (typeof par ===’number’){return 0;}
if (typeof par ===’string’){return 0;}
return par.length;
}
The error message I'm receiving is:
Bummer! You're missing a function named 'arrayCounter'
I think the error message I'm receiving is incorrect. Will you please help?
7 Answers
Anthony Garritano
Courses Plus Student 2,165 PointsI believe you need to use else if statements here.
I've been dabbling pretty heavily in PHP/MySQL of late so my JS is a little rusty. I haven't tested this but I believe it's correct.
function arrayCounter(par){
if (typeof par ===’undefined’){return 0;}
else if (typeof par ===’number’){return 0;}
else if (typeof par ===’string’){return 0;}
return par.length;
}
Anthony Garritano
Courses Plus Student 2,165 PointsSo it looks like your code was using backticks ( ` ) instead of single quotes ( ' ) or double quotes ( " ).
Swap those out and you should be good to go!
Gabriel Brewdant
783 PointsGood eye! That definitely helped. The colors of the code changed to more appealing colors... However, the code is still not being cleared as correct. Here's what I have:
function arrayCounter(par){
if (typeof par ==='undefined’){return 0;}
else if (typeof par ==='number’){return 0;}
else (typeof par ==='string’){return 0;}
return par.length;
}
Gabriel Brewdant
783 Pointsapologies. Below is the code I am trying. No backticks are being used, but the same error message is being given:
function arrayCounter(par){
if (typeof par ==='undefined'){return 0;}
else if (typeof par ==='number'){return 0;}
else (typeof par ==='string'){return 0;}
return par.length;
}
Gabriel Brewdant
783 PointsSUCCESS! It was the backticks originally that was causing the issue, and then the last "else" needed to be an "else if" (in the code immediately above).
Thank you Anthony!
Anthony Garritano
Courses Plus Student 2,165 PointsAnytime ;)
Anthony Garritano
Courses Plus Student 2,165 PointsAlso, I wanted to mention in this particular instance you don't technically have to use else if statements, the solution will work without them.
You want to use else ifstatements when a group of statements are related to one another. I believe this lesson just showedifstatements to keep things simple, but I would say this is a situation where you would want toelse if. Anyone coming after you will have an easier time understanding what is going on,else if`s show an implicit relationship.
Les Campbell
26,987 PointsI'm having similar issues with same Code Challenge. I honestly don't think the videos went over enough information before the Code Challenge.
Liz Karaffa
17,576 PointsI agree. The challenge was a jump for me after watching the videos. If I hadn't come on here and seen what everyone else did I never would have came to that conclusion.
Les Campbell
26,987 Pointsfunction arrayCounter(array) {
if (typeof array === ’undefined’) {
return 0;
};
if (typeof array === ’number’) {
return 0;
};
if (typeof array === ’string’) {
return 0;
};
return array.length;
}
Gabriel Brewdant
783 PointsAre you getting the same error message? ("You're missing a function named 'arrayCounter' ").
Anthony Garritano
Courses Plus Student 2,165 PointsYou have semicolons after each if statement. Remove those and you should be good.
Gabriel Brewdant
783 PointsI've referred the question to support. Hopefully they respond...
Anthony Garritano
Courses Plus Student 2,165 PointsCan you copy and paste everything that you have for that challenge?
Greg Shilling
3,723 PointsHi Gabriel,
You've already solved the problem, but for future reference, this is how you should use the if else statement:
function exampleFunction(myVar) {
if (myVar === "something") {
return "something";
} else if (myVar === "an other thing") {
return "something else";
} else {
return "all other cases";
}
}
So, basically, you're saying: if (the first case) { then this } else if (the other case) { then this } else if (you can add as many of these statements as you want) { then this } else (in all other possible cases not clarified in the above statements) { then this}
You can use single quotes ( ' ) and double quotes ( " ) as long as you're consistent and always open and close with the same ones.
Alternatively, you can also use switch-case statements if you've got a lot of specific cases :-)
Gabriel Brewdant
783 PointsGabriel Brewdant
783 PointsGood idea, and you're probably right. I double checked your syntax against w3schools.com and it seems legit. Unfortunately, the code challenge is still spitting the same error. Here's the link to the challenge: http://teamtreehouse.com/library/return-values