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

array length

Getting " If I pass in an array in to 'arrayCounter' of '1,2,3' I get 0 and not the number 3." on this code. I'm sure array.length is the right method for getting length of arrays, Help! what am i doing wrong?

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

1 Answer

Gareth Borcherds
Gareth Borcherds
9,372 Points

Your if statement is written incorrectly. Try using:

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

When evaluating more than one condition of a function you must use if (statement1 && or || statement2)

&& and means that both conditions have to be validated to show true. || means that either statement has to be evaluated. You want the or statement in this one.