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

Nikki Turrisi
Nikki Turrisi
15,095 Points

I am struggling with this challenge question

I know my syntax is wrong, but I can't figure out why. I am stumped. Any help?

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 (1, 2, 3);
        return arrayCounter.length;
      console.log(arrayCounter);
        if(arrayCounter === 'undefined'); 
        return 0; 
      console.log(arrayCounter);

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

5 Answers

Jesus Mendoza
Jesus Mendoza
23,289 Points

Hi!

function arrayCounter (1, 2, 3) {
        return arrayCounter.length;
}
      console.log(arrayCounter);

if(arrayCounter === 'undefined') { 
        return 0;
}
      console.log(arrayCounter);

You're using semi-colons instead of curly brackets

B Nawaz
B Nawaz
13,402 Points

Indeed! Like Jesus said, you need to use curly braces for your function definition, as such:

function (param1, param2) { //outer curly braces //do some stuff

var curlyBracesUsed = true;

if (curlyBracesUsed) { //inner braces
     return "Then everything works!";
} //inner braces

} //outer curly braces

If this helped, mark me right, I want some of those sweet sweet forum points! :D

Varuzhan Darbinyan
Varuzhan Darbinyan
8,360 Points

This should work ;)

function arrayCounter(array) {
        if (typeof(array) === "string"){ 
          return 0;
        }
        else if (typeof(array) === "number") {
          return 0;
        }
        else if (typeof(array) === 'undefined') { 
          return 0;
        }
              else {
          return array.length;
          }
      }
Edmund lok
PLUS
Edmund lok
Courses Plus Student 6,465 Points

Hey maybe this will help!

<script> function arrayCounter (array) { if (typeof != 'object'){ return 0; } return array.length; } </script>

Edmund lok
PLUS
Edmund lok
Courses Plus Student 6,465 Points

Hey maybe this will help!

<script> function arrayCounter (array) { if (typeof != 'object'){ return 0; } return array.length; } </script>