Diff Two Arrays- Why doesn't this code work?

function diffArray (arr1 ,  arr2) {

  var newArr = [ ];

  for (var i = 0 ;  i < arr1.length ;  i ++){

    if ( arr2.indexOf ( arr1 [ i ] ) === -1 ) {

      newArr .push ( arr1 [ i ] );

    }

for ( var j= 0;  j<arr2 .length;  j++) {

  if  ( arr1 .indexOf (arr2[j])=== -1 ) {

newArr.push(arr2[j]);

  }

}

  }

  return newArr;

}

What challenge are you working on?

Also, all those extra spaces in your code (newArr .push ( ...) make it very difficult to read.


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 (’).

Intermediate Algorithm Scripting: Diff Two Arrays

Can you please provide the link to the challenge?

Knowing which tests are failing and the results of any sample inputs you have tested would help, if you have it handy.

Learn Intermediate Algorithm Scripting: Diff Two Arrays | freeCodeCamp.org

One thing that I really think will help you is if you format your code better. Make sure all the curly braces line up properly. In the process of doing this I think you’ll find your error.

1 Like

Got it, thank you!!!