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

Help with Code Challenge: Return Values

The code challenge reads: Around line 17, create a function named 'arrayCounter' that takes in a parameter which is an array. The function must return the length of an array passed in or 0 if a 'string', 'number' or 'undefined' value is passed in.

My Code

  function arrayCounter (1,2,3) 
      {
       return arrayCounter.length; 
      }
      if (arrayCounter == 'string' || arrayCounter == 'number' || arrayCounter == 'undefined')
      {
        return 0;
      };

The error reads: Bummer! You're missing a function named 'arrayCounter'

Can someone also explain how to format code in this forum.

I know these are probably two easy tasks, but have been frustrating. Thanks for your help.

When using the markdown of three back ticks, you need to have a blank link before, then three back ticks on their own line, then your code, then 3 more back ticks, then again a blank line.

5 Answers

The problem in your code is the following:

  • you are hardcoding 3 arguments, instead of accepting a variable, in the function declaration.
  • you are inspecting the function itself for length, etc. -- you need to inspect the argument variable that contains the array instead.
  • you don't need the ending semi-colon at the end of a function after the curly braces.

I think fixing these three issues will get you back on track. :) Let us know how it goes -- good luck!

Hello Atiba.

Here's the code explained.

 // You recieve a variable that contains your array
function arrayCounter(array) {
  // Then check the type of the variable with the 'typeof' operator
  // If it's not an array you return 0 (Use the '||' operator to check for multiple values)
  if(typeof array === 'string' || typeof array === 'number' || typeof array === 'undefined') {
    return 0;
  }
  // If the variable was not a string, a number or an undefined variable, return its length property
  else {
    return array.length;
  }
}

Hope this helps.

Awesome, thanks guys. That makes much more sense.

Thanks, Victor. This helped me too.