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

JavaScript Foundations - Stage 5 Functions - Challenge Task: Having trouble with this excercise.

I feel like I'm doing what the question is asking but I keep on getting an error message stating that a "0" is being returned instead of a numerical value. I think my problem is with the syntax in checking for the "string", "number" & "undefined" conditions. Are we literally supposed to type them out this way? Any help would be appreciated, thanks.

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>
      var x = [];
      function arrayCounter(x){

        if (typeof x === 'string', 'number', 'undefined'){
          x = 0;
        }else{
          return x.length;
        }
      }
    </script>
  </body>
</html>

2 Answers

chris slaats
chris slaats
5,996 Points

they asked you to find the variable array so instead of creating your own variable of x just use array as your conditon to the function. Also instead of using coma's use the OR operator which is || it checks all the typeof's in a single section so it will check for the string OR the number OR undifined. Finally when returning a value dont use your variable in your case x to return 0, use the return operator which will return the whole function to 0 instead of just your variable. I'm not the best at explaining but i hope that this helps alittle and it works and sorry the code isnt fully in the box not sure how to quote code properly.

function arrayCounter (array){

    if (typeof array === 'string'||typeof array ==='number'||
        typeof array ==='undefined'){
        return 0;
    }       else { 
        return typeof array.length ;
   }
Clayton Perszyk
Clayton Perszyk
Treehouse Moderator 48,850 Points

hi chris

When you return the array's length, you don't want to use the typeof keyword; it should just be:

return array.length;

Thanks,

Chris - explanation was great, it worked perfectly and helped me understand the concept.

Clayton - thanks for that, I was wondering if the "typeof" was necessary in a return statement.

Edmund lok
PLUS
Edmund lok
Courses Plus Student 6,465 Points

this should be ok!

<script> function arrayCounter (array) { if (typeof != 'object'){ return 0; } return array.length; } </script>