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!

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


failing to understand the code requirement

<!DOCTYPE html>
<html lang="en">
    <title>JavaScript Foundations: Functions</title>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
    <h1>JavaScript Foundations</h1>
    <h2>Functions: Return Values</h2>

      function arrayCounter(){
        if (typeof arrayCounter === 'undefined'){
          arraycounter.length = 0;
        } Else{
      return array.length;
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
Jonathan Grieve
Treehouse Moderator 91,247 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. :-)