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

Richard Barkinskiy
Richard Barkinskiy
10,663 Points

It's returning 0 like the instructions say, but still red error...

Here's my code so far:

  function arrayCounter(x){
    if (typeof x === 'undefined') {
      return 0; 
    }

    return x.length;
  }

  arrayCounter(x);

It's returning a 0 in the console per instruction, but still not passing it.

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(x){
        if (typeof x === 'undefined') {
          return 0; 
        }

        return x.length;
      }

      arrayCounter(x);



    </script>
  </body>
</html>

1 Answer

Andrew Kiernan
Andrew Kiernan
26,892 Points

Hi RIchard!

The challenge wants you to check if x is undefined OR a string OR a number, so you need to check to see if it is 1 of those 3 values. There are a couple of ways to do this, like multiple if-statements, or you can use the logical or ( || ) and repeat your typeof check 3 times in one if-statement.

Hope that helps. Let me know if you have any questions!

-Andrew

Richard Barkinskiy
Richard Barkinskiy
10,663 Points

Thanks for clearing it up for me. Works now!