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

Steve Hale
Steve Hale
5,216 Points

Code Challenge

I can't correctly respond to the code challenge that says this:

"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 best response is this, I think the problem is that I don't know what to put as the parameter before ".length":

function arrayCounter() {.length; if (typeof === 'string', 'number', 'undefined') {return 0;} }

Please help.

3 Answers

"or" characters are marked by two repeating "|" symbols placed between the two values. example:

''' 'this' || 'that' // evaluates to "this" or "that" '''

I also recently had to spend some time on that one and get some help. I will tell you that the biggest point after declaring your function is using the typeof() method to pass in your argument. here's a link to the forum help I got when I posted on this. Should clear things up for you (https://teamtreehouse.com/forum/cant-seem-to-get-this-right)

good luck

btw - the arguments don't need to exist outside the function. I saw an example where they did and it looked successful (the member created an array outside of the function and then passed it in and it accepted the answer), but I wanted to leave it open as if it was to accept an argument or parameter later on and it was just fine.

Steve Hale
Steve Hale
5,216 Points

Thanks Joe! Very Helpful!

Patrick Metcalfe
PLUS
Patrick Metcalfe
Courses Plus Student 7,563 Points

A method header needs to look like this:

function arrayCounter(parameters) {
//Code Goes Here
return something;
}

So the challenge wants you to make a function that takes a parameter than checks weather it is an array and execute one line of code, or execute another line if something else is true. Also remember that or in JavaScript is represented by ||. Moreover the length method is a property of an array. So it must be done on an array, like so: myArray.length

Hope this helps you figure it out good luck. And just comment if you have any further trouble with this challenge.

Steve Hale
Steve Hale
5,216 Points

Thanks Patrick. Part of what I don't understand is what the parameter should be. I also don't know how to make the "or" characters.