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

Patrick Ludwig
Patrick Ludwig
2,462 Points

OK. I checked the videos 3 times, and I cannot pass this challenge

I keep getting the error message that undefined is not 0. I did the if typeof array === undefined just like in the video. This still does not make it 0 if it is a string or a number like it asks. I am very confused and cannot get passed this no matter what I do

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(array){
       if ( typeof array === 'undefined'){
          array = 0;
       }
        console.log(array);
        return array.length;
      }


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

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Patrick,

The challenge wants you to return 0 if the type is a string, number or undefined. In this case you should use an if statement like this.

      function arrayCounter(array) {
        if (typeof array === 'undefined' ||
            typeof array === 'string' ||
            typeof array === 'number') {
            return 0;
        }
      }

Otherwise, you'll need to return array.length.

I hope this helps.

Zoltán Holik
Zoltán Holik
3,401 Points

Try this:

 function arrayCounter(array){

        if(typeof array === 'undefined' || typeof array === 'string' || typeof array === 'number'){
          return 0;
        } else {
          return array.lenght;
        }
      }

      var array = [0,10,20,30,40,50];
      arrayCounter(array);