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

I'm having some trouble with the return function question , help!

hey, I'm stuck trying to figure out the correct syntax for the function question in the js foundations functions question about returning the length of the array if the array is a number string or undefined. Any help would be appreciated!

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 array=["1,"ten", undefined]

       return(array.length);

      }


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

1 Answer

Hi Mandana,

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.

Almost, we need to create function named arrayCounter that takes "array" in a parameter, and the value passed is "string" OR "number" OR "undefined" which will return 0. To make it easier, we can combine the 3 blocks into one line to use || (means OR). Then we return the length of the array in the end of the line.

Here's simple solution:

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

Hope you get it. :-)