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

Problems with first code challenge in JavaScript foundations, functions

in code challenge we are ask to create function arrayCounter

hear is my code, but it is not working :

function arrayCounter (x){
if(x.length ==='string' || x.length==='number' || x.length==='undefined'){
return 0;
}
return x.length;
}

var x = [];
arrayCounter(x);
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(x.length === 'undefined'){
          return 0;
        }
        if(x.length === 'string'){
          return 0;
        }
        if(x.length ==='number'){
          return 0;
        }
        return x.length;
      }

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

1 Answer

geoffrey
geoffrey
28,736 Points

A possible way to handle this problem might be found here If you want some more solutions, please, use the search input. You should even do that before asking your questions, often there is already a good answer given.