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 JavaScript Foundations Functions Return Values

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Arrays in functions

Here is the challenge that i'm struggling with: 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.

The problem i'm having is knowing how to create a function that only takes in a parameter that is an array?

Please help.

4 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

You're almost there. Couple of things

  1. a single = is an assignment operator. You need to use == or === to test a condition. So,
if (typeof array === 'string') {
  return 0;
}
  1. You don't need the else in there.

  2. You're not sending back the length of the array, which is named array. You're trying to send back the length of the function, which won't work. You need

return array.length;

Hope that helps.

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Create a function that accepts just a single argument. Then in the function, you need to test if that arguments is a string, number or undefined. You can use the typeof operator to check what type of object the argument is. For example, typeof 'word' returns the value string.

Nthulane Makgato
Nthulane Makgato
Courses Plus Student 19,602 Points

I think that you would know better than I would(since you are staff) but my understanding is that, one should check for the terms - "string", "number" or "undefined" not necessarily their types.

But here's what i've tried since you posted your comment:

function arrayCounter(array) {
    if (typeof array = "string") {
        return 0
    }
    if (typeof array = "number") {
        return 0
    }
    if (typeof array = "undefined") {
        return 0
    }
    else return arrayCounter.length;

}

The response I get is, "Bummer! If I pass in an array in to 'arrayCounter' of '1,2,3' I get 0 and not the number 3."

I would greatly appreciate your input.

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Thank you very much Dave. It worked. I definitely have a much better understanding of what's going on.

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Hi Dave, i've just posted a question in Forum about JavaScript and I think that you can help. Please look it up and assist if you can.