Need help on Diff Two Arrays

Need help on Diff Two Arrays
0

#1

https://www.freecodecamp.org/challenges/diff-two-arrays.

Can I get help in following code or link is above. I am getting err messgaes as seen but do not understand how to solve them. I alos do not know if it works.

function diffArray(arr1, arr2) {
  var newArr = arr1.concat(arr2).sort(); //join arr1 and arr2 then sort.
 
  var myArr = [];
  
  for(var i = 0; i < newArr.length; i++){
    var x = newArr[i]; 

//value at index i.
  //on following code my errors are 'bad assignment" and "expected a conditional expressions and instead saw an assignment" which I can not understand.  
   
 if(newArr.lastIndexOf(x) = i){ 

 //in the same newArr find out last reoccurance of value at i, if it is same then 'push' in myArr.

      myArr.push(x);
      
    } //if..lastIndexOf is closing here

    return myArr;

  }//for loop is clsing here
  
}//function
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

#2

The error is:

SyntaxError: Invalid left-hand side in assignment expression (12:3)

Your line 12 is:

if(newArr.lastIndexOf(x) = i){

Remember that “=” is used for assigning a value on the right side of it to a variable name on the left side of it. You are attempting to assign the value of i to newArr.lastIndexOf(x). What you need to review is how to use comparison and logic operators.

Once you figure out what operator to use here, you will not have any console errors, but your solution will still not work correctly. One thing you need to think about is where your return statement should be located. Remember, as soon as you return, you immediately exit the function regardless if a loop was not complete.


#3

Thanks. I corrected that. Now I do not syntex error. But my logic is not giving right answer. Can I get help please.


#4

Make sure to read my message about where the return statement should be located. When you have that figured out, repost your code here, but make sure to put three backticks on the line before and after your posted code. See below for how to do this.