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

Dave Figueroa
Dave Figueroa
13,912 Points

Looking for a function?

The exercise requires that a function, arrayCounter, evaluate an input and return the length of the array - or return 0 if it's not an array.

I've created the arrayCounter function and passed all the non-array test cases.

When the array [1,2,3] is inputted, my function returns what should be the correct answer: 3

However I get a failed result: "Bummer! '3' is not a function!"

Why is the grader looking for a function as the returned result?

Am I missing something or is it broke?

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

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

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Dave;

I think you should be using if... else if statements, or you could do something like:

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

Happy coding,

Ken