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

JavaScript

Kyle Brooks
Kyle Brooks
6,753 Points

Code 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

Anytime. Sometimes the wording of the objectives gives me more issues than the code does.

Kyle Brooks
Kyle Brooks
6,753 Points

for sure. I can figure out the basics of arrays, but answering the "Word problem" is the hardest part.

function 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
James Barnett
39,199 Points

Callum Smith -

Remember 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.

At 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
James Barnett
39,199 Points

I 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.

Yes, 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
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
Valery Kukatov
6,996 Points

I 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
Leyla Movahedi
6,549 Points

I had the same question, however I think it has to do with the typeof word in the beginning of the ().

Kyle Brooks
Kyle Brooks
6,753 Points

you'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
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
Michael McLaughlin
14,033 Points

I 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
Reuben Varzea
23,182 Points

If/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