SyntaxError: unknown: Unexpected token, expected ")"

I’m almost done with this challenge, I’m getting a syntax error, can anybody pinpoint it out? Below is my solution.


function diffArray(arr1, arr2) {
  var result = [];
  for (var i=0; i<arr1.length; i++){
    if(arr1.indexOf(arr2[i]) === -1){
      result.push(arr2[i]);  
  }
    }
    for (var j=0; j<arr2.length; j++){
      if(arr2.indexOf(arr1[i] === -1){
        result.push(arr1[j]);
       }
       
      }
      return result;
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

You didn’t close out your indexOf

1 Like

I like to use jsfiddle to format code if I don’t have anything set up, and…
image

2 Likes

Thank you, so hard for the average human eye to catch!

As @ilenia pointed out, when you use a good editor that supports color coded linting and such then it will catch most of these errors for you.

1 Like

Got it, I’m mainly using freecodecamp interface for coding, and repl when I need to test. But you’re right, it would make my life a lot easier.

As a future debugging tip, Unexpected Token almost always means that you forgot a closing }, ), ], ', or ". Sometimes it’s a misplaced semicolon.

Those symbols are called “tokens” and what the error is telling you is that it was expecting a specific token and it found a different one that shouldn’t be there before the one it’s expecting. The error message will have a line number, but in this case it’s not amazingly helpful because it is telling you the line that the unexpected token was on, rather than which opening {, (, [, ', or " is missing its partner.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.