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

Victoria Rast
Victoria Rast
7,751 Points

Function returns in JavaScript Foundations Code Challenge

I am confused as how to create this, here is the question:

"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. "

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() {

      }

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

2 Answers

Hey Victoria,

A few things to keep in mind with this challenge are: 1) to be sure to use a name for the variable you are going to use in the function i.e. function arrayCounter(arr) where arr can be almost any name you can come up with; 2) You cannot check to see if a variable is specifically an array because arrays are really just very specific objects, but you can check to see if it is anything else; 3) Remember to use the length method on the array variable you use when you created the function i.e. in the above function, I used arr so you would use arr.length.

Try to play around with the code and see if you can figure it out. If you get too frustrated, check back here, and I'll give you the answer and explain it. I don't want to give you the answer off the bat so that you can figure out the solution for yourself and get a better understanding of coding. :)

Victoria Rast
Victoria Rast
7,751 Points

Hi, sorry for taking this long, due to work and obligations I haven't got back to it until now. Thanks for the info and stuff, I have played and played with it, but I am still having trouble. I am uncertain on how to check to see if they are passing undefined, string or number:

function arrayCounter(arr) { if ('undefined', 'string', 'number' ) { return 0; }else {
return arr.length; } }

Well, the challenge wants you to get familiar with using the command typeof to identify what a particular variable is. typeof can identify several types of variables including string, number, object, null, undefined, etc.; it cannot identify an array as being an array because all arrays are are just specific objects that use numbers. When you execute typeof, it returns a string containing what type of object that particular variable is.

In order to use the typeof command, you just type out typeof and then the variable and then you can compare it to a string containing what types of objects you're looking for. So, with that in mind, here is the code for the challenge:

      function arrayCounter(arr) {
        if (typeof arr === 'string' || typeof arr === 'number' || typeof arr === 'undefined'){
          return 0;
        }
        else {
          return arr.length;
        }
      }
Victoria Rast
Victoria Rast
7,751 Points

Thanks, I never thought of an individual 'typeof'.

My pleasure, Victoria! Happy Coding! :)