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

raj kanwar
raj kanwar
4,043 Points

Cant figure out how to check for undefined.

I have this code which is appended but it doesnt work. I cant figure out what the mistake is.

The message on top is that I am not checking for undefined.

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JavaScript Foundations: Functions</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Functions: Return Values</h2>
    <script>

      function arrayCounter (a){
        if (a.isArray)
        {
        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>
  </body>
</html>

2 Answers

I think the if(a.isArray) is not working as it should

here is the code that works

  function arrayCounter (a){
    if (a instanceof Array)
    {
    return a.length;
    }
  else  
  {
  return 0;
  } 
  }
Michael Escoto
Michael Escoto
30,028 Points

It should work if you remove the quotation marks around undefined.