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

Caitlin Cadieux
Caitlin Cadieux
2,374 Points

Can't figure out why array length won't return.

Been working on this for a little bit and I'm completely stumped. Not sure what's going wrong but I can't get the array length to return (I'm not sure I'm getting any of this to actually work correctly). Can anyone shed some light?

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 (thing) {
        if (typeof thing === 'string', 'number', 'undefined') {
          return 0;
        }
        return thing.length;
      };

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

1 Answer

Gareth Borcherds
Gareth Borcherds
9,372 Points

You have to write out each statement separately and connect them with || to simulate OR. Like this:

function arrayCounter (thing) {
        if (typeof thing == 'string' || typeof thing == 'number' || typeof thing == 'undefined') {
          return 0;
        }
        return thing.length;
      };
Caitlin Cadieux
Caitlin Cadieux
2,374 Points

I'm actually getting a syntax error with this for some reason. The main error I get back with the code in my post is that the array length is returning as 0 for some reason as well...

Gareth Borcherds
Gareth Borcherds
9,372 Points

I just ran this in the console and it worked fine. What other code are you using with it?

Caitlin Cadieux
Caitlin Cadieux
2,374 Points

that's it, I just copy pasted your code in to see what would happen and it returned a syntax error :(

Caitlin Cadieux
Caitlin Cadieux
2,374 Points

welp! Just handtyped it in and somehow it worked. Thank you so much! Not sure what exactly went wrong there but I'm sure it was user error, haha.