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

Christian Lawrence
Christian Lawrence
3,941 Points

Return Values

I can't work out what I'm doing wrong here, seem to keep getting the wrong return value.

The function must return the length of an array passed in or 0 if a 'string', 'number' or 'undefined' value is passed in.

Bummber! Wrong value returned from 'arrayCounter("string")' it should be 0

function arrayCounter (dog) { if (typeof dog === 'undefined'){ return 0; } return dog.length; } arrayCounter('bulldog');

2 Answers

Ron McCranie
Ron McCranie
7,837 Points

It wants you to test if it's undefined, string, or number. Try this:

function arrayCounter(stuff) {
        if(typeof stuff === 'string' || typeof stuff === 'number' || typeof stuff === 'undefined') {
          return 0;
        } else {
          return stuff.length; 
        }
      }
Christian Lawrence
Christian Lawrence
3,941 Points

Cheers I understand now, didn't know about the ||