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

Dandy Cheng
PLUS
Dandy Cheng
Courses Plus Student 2,613 Points

Why does it say parse error? I'm pretty sure I have the syntax right.

I followed how the others did it from the other thread. But instead I changed the name of the parameter to "arr". Why does it keep saying parse error?

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(arr){
        if(typeof arr === 'undefined' || typeof arr === 'string' || typeof arr === 'number'){
            return 0;
        }else{
            return arr.length;
        };

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

3 Answers

Stone Preston
Stone Preston
42,016 Points

you can remove the semicolon after the closing bracket of the else (the closing curly bracket is already a termination character, so the semicolon is not necessary) and add the closing bracket for the end of the function

function arrayCounter(arr){
        if(typeof arr === 'undefined' || typeof arr === 'string' || typeof arr === 'number'){
            return 0;
        }else{
            return arr.length;
        } //remove semicolon
  } //add closing bracket to close the function
Chris Shaw
Chris Shaw
26,676 Points

Hi Dandy,

You're simply missing the closing curly brace from your IF statement, in saying that because you're either returning 0 or the array length the else statement is redundant.

function arrayCounter(arr) {
    if (typeof arr === 'undefined' || typeof arr === 'string' || typeof arr === 'number') {
        return 0;
    }

    return arr.length;
}

Happy coding!

Stone Preston
Stone Preston
42,016 Points

i actually kind of like keeping the else since its a bit more explicit about whats going on. if this return that, else return this other thing. While it might not be necessary, I dont really find it redundant. it adds some context to the function as a whole

Chris Shaw
Chris Shaw
26,676 Points

It is more explicit but I to me it serves no purpose when using return statements but I think it's just my preference after 5 years of web development.

Dandy Cheng
PLUS
Dandy Cheng
Courses Plus Student 2,613 Points

Thank you! I found my mistake and it was the function's closing curly braces.

Stone Preston
Stone Preston
42,016 Points

cool glad you got it working