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

What do?

So the problem is: "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."

I coded the following:

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

It's telling me: "Bummer! If I pass in an array in to 'arrayCounter' of '1,2,3' I get 0 and not the number 3." In my mind this should return the length of the array unless it's a 'string', 'number' or 'undefined', in which case it will return "0". What gives?

3 Answers

Hi Drew,

Seems like a srewy code challenge. I tried double quotes. I tried an else statement, anyhow this finally passed:

function arrayCounter (array) {
  if (typeof array === 'string' || typeof array === 'number' || typeof array === 'undefined') {
    return 0;
  }
  return array.length;
}
James Barnett
James Barnett
39,199 Points

> Seems like a srewy code challenge. I tried double quotes. I tried an else statement

I tried single quotes, double qoutes, else statement all worked fine. Although without you posting your code I can't tell you where you issue is.

However it is possible to make a semantic error with the code and not know it as their is no console.log.

So I made up little demo feel free to try out any ode that doesn't work in the code challenge.

Thanks a lot, guys. This was the first one that stumped me. I get why it didn't work now. Thanks for your help!

You are on the right track, but your if condition is incorrect. Use and (&&), or (||) logical operators, not commas to list additional conditions:

if ( someVar === somethingElse || someVar  == anotherThing) {
    // some code
}

Thank you!

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

Hi Drew,

I've just tried your solution in my text editor and javascript, it doesn't seem to acknowledge arrays, so everything one put in the arrayCounter is 0 even if its not a string, number or undefined. I'm not sure why this is the case.

Anyway, I suggest you compare them individually.

e.g.

function arrayCounter(array) { if (typeof array === "string") { return 0 }

  if (typeof array === "number") {
       return 0
  }

  if (typeof array === "undefined") {
       return 0
  }

  return array.length;

let me know how it goes.