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

functions

failing to understand the code requirement

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(){
        if (typeof arrayCounter === 'undefined'){
          arraycounter.length = 0;
        } Else{
      return array.length;
        }
      }
    </script>
  </body>
</html>
Happy .
Happy .
11,414 Points

you have an upper case E on Else, it should read else. as for any other problems i'm not sure sorry

Can you please choose a best answer, Prudence? Thank you!

2 Answers

Hey Prudence,

This challenge wants you to check whether an argument within arrayCounter is an array or not, and if it is, return the length of the array, otherwise, return 0. You have to have an argument for the arrayCounter to check so you need to put a variable name within the function, like "arg" which is just short for "argument".

Since you can't check whether a variable is an array or not (because arrays are actually just specific objects), you can, however, check to see if it is a string, number, or undefined. That means you need to add two more conditions to your if statement.

You also need to just return 0, not do whatever it is you're attempting to do in the if statement. So, your code should look like this:

      function arrayCounter(arg){
        if (typeof arg === 'undefined' || typeof arg === 'string' || typeof arg === 'number'){
          return 0;
        } else{
      return arg.length;
        }
      }
Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

The function is checking for a string of certain characters but it first wants to know if there is a string to be read. If it isn't a string it'll return because there are no characters but if there is a string it'll send back to the function how many characters there are in the string... in other words the length of the string. :-)