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 trialJason Ratzlaff
Courses Plus Student 2,612 PointsDon't understand what you want.
You guys are asking for: The function must return the length of an array passed in or 0 if a 'string', 'number' or 'undefined' value is passed in.
I believe this is getting the results your asking for. I have the code running on my local machine and have confirmed it in console. What am I missing?
function arrayCounter (color,manufacturer){
if (typeof color === 'undefined'){
return 0;
}
if (typeof manufacturer === 'undefined') {
manufacturer = ("Car");
}
car = color +" " + manufacturer +"s" + " " + "are bad";
return car.length;
}
arrayCounter ("red","ferrari"); arrayCounter ("red"); arrayCounter ();
4 Answers
Laura Cressman
12,548 PointsHi Jason, I'm not sure what's going on with the color and manufacturer stuff, the question just wants you to return the length of an array (if an array is passed in), or 0 (if something else is passed in). Something like this should do the trick. Does that make sense? Smile more, Laura :)
function arrayCounter (list) {
if (typeof list == "string" || typeof list == "number" || typeof list == "undefined") {
return 0;
} else {
return list.length;
}
}
Jason Ratzlaff
Courses Plus Student 2,612 PointsWell I do have experience with PHP so ya. But this is no way related to the lesson video previous to the question. I was trying to keep my train of thought consistent with the lesson and not go outside of the scope of what was being taught.
We haven't been introduced to OR at this point and also have not been made clear we could use typeof this way. As an experiment I tried to do typeof list == "array" it didn't seem to work. Just for the sake of learning would you be able to do this with an array and if so how?
Also in the previous video he was using === and single quotes. Can you give me more detail as to why you're using == and double quotes in this example?
Laura Cressman
12,548 PointsI think there must be a way to do it with an array, I'll look it up and get back to you. As for the single/double quotes, they mean the same thing and it's just a matter of preferences. The === is more specific than ==, meaning you can use === if you want to check if something is an exact match, but == if you want it to be close enough. In this example it doesn't make a difference, but for another circumstance if you check "null == undefined" it will return true but "null === undefined" will return false.
Ben Lockett
22,419 PointsHi Jason,
Laura's code is more of a short hand of what they showed you in the video. We were shown If else statements so instead of the || i would of done something like this,
function arrayCounter (list) {
if (typeof list == "string") { return 0; } if (typeof list == "number") { return 0; } if (typeof list == "undefined") { return 0; }
else { return list.length; }
}
This worked for me although i would normally of used ||.
Hope this helps
Jason Ratzlaff
Courses Plus Student 2,612 PointsOk cool thanks Ben and Laura. As i thought but wanted to confirm. It's similar to PHP then where single and double quotes most of the time are a matter of preference.