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 trialKyle Brooks
6,753 PointsCode Challenge: JavaScript Foundations - Return Values - Challenge 1 of 1
Task: 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.
URL: http://teamtreehouse.com/library/return-values
Code I tried:
<script>
function arrayCounter (string, number) {
if (typeof string === 'undefined') {
return array.length;
}
if (typeof number === 'undefined') {
return 0;
</script>
Message: "Bummer! You're missing a function named 'arrayCounter'"
9 Answers
Callum Smith
13,715 PointsAnytime. Sometimes the wording of the objectives gives me more issues than the code does.
Callum Smith
13,715 Pointsfunction arrayCounter(array) {
if (typeof array === "string") {
return 0;
} else if (typeof array === "number") {
return 0;
} else if (typeof array === "undefined") {
return 0;
} else {
return array.length;
}
}
Give that a go, it should work.
James Barnett
39,199 PointsRemember our goal here on the forum is give help not answers, next time try giving an explanation and/or a hint instead.
Need more explanation on the distinction check this out.
Sako Kassabian
13,796 PointsAt what point does the training talk about else if? I was totally lost there. There aren't any hints either, so you're totally in the dark.
James Barnett
39,199 PointsI just checked, in this course, if/else statements actually aren't covered.
There is however a video covering them in the control structures stage of the Introduction to Programming course apparently.
Sako Kassabian
13,796 PointsYes, that is true, but the answer above mentions the "else if" which isn't explained anywhere thus far. So I was a bit confused to think that that was the correct answer but it wasn't ever explained. After some googling, I now see the difference between the if and the else if.
James Barnett
39,199 Points>
The answer above mentions the "else if" which isn't explained anywhere thus far
As in programming there's more than one way to do it (TIMTOWTDI, pronounced “Tim Toady”), so someone might choose to post code that solves a problem using techniques not yet discussed. That's one of issues that arise when Treehouse students choose to give answers instead of help.
Valery Kukatov
6,996 PointsI am using the Deep Dive of JavaScript and I was able to complete this challenge with just if statements. I did use though the above answer by Callum Smith as reference. Even though I am aware that there are if/else if/else arguments/statement checkers, it's good practice to figure out how to do it with what the actual video teaches. This exercise only really confused me as to when it asked to make function return 0 if it is undefined, number, or string. When you are checking if an array is equal to any of those 3, you are marking them with "xxxx" or 'xxxx'. Yet doesn't that make the function look at number, undefined and string as just strings? From my understanding that anything you type in the quotes turns into a string. Am I missing something or should I start a different discussion on this?
Leyla Movahedi
6,549 PointsI had the same question, however I think it has to do with the typeof word in the beginning of the ().
Kyle Brooks
6,753 Pointsyou're the bomb. Thanks man! JS is definitely not my forte, but trying to learn what I can in the Web Design track.
James Barnett
39,199 Points>
Am I missing something or should I start a different discussion on this?
Yes you are missing something. However it's a good topic for its own discussion.
Michael McLaughlin
14,033 PointsI wrote this: but it did not work:
function arrayCounter(array){
if( array.isArray() ){
return array.length;
}else if( array == undefined ){
return 0;
}else{ return 0; }
}
Reuben Varzea
23,182 PointsIf/Else is discussed in the Intro to Programming Deep Dive, so probably why they don't get in to it more in the Javascript Deep Dive (that's my guess, anyhow).
For me, I didn't bother with if/else. I just used an if statement and then used a logical operator (|| for 'or') to check for one of the three types that should return 0. I thought it made life a little easier.
Here's the link to the Intro to Programming DD - http://teamtreehouse.com/library/introduction-to-programming
Kyle Brooks
6,753 PointsKyle Brooks
6,753 Pointsfor sure. I can figure out the basics of arrays, but answering the "Word problem" is the hardest part.