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

varis darasirikul
varis darasirikul
10,832 Points

Troubles with JavaScript Foundations,Function Challenge task

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 wrote: <script>

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

</script>

it doesnΒ΄t work. and i don't know what wrong?

5 Answers

Ok.. I got it to pass with this:

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

Good job solving your own problem, that's the best way to learn.

Remember, we're here to give help, not answers. Next time instead of just sharing the end result, why don't you tell us about how you figured it out. For instance ... use a logical or inside of the a single if statement would have made a great hint.

I'm also stuck on this, doesn't seem the instructions are very clear.. also using an "object" doesn't seem logical seeing that in this track we haven't even covered them.. anyone else have a suggestion or can clarify?

the || stands for "or"

Hi you need to use 'object' and not 'array' and it should work.