I have code that checks for similar values in two arrays, and if there are any, they are not displayed in the result. However, when I switch the lengths of the arrays so that Array2 is longer than Array1, I end up with an empty result array. How can I achieve the same result regardless of which array is longer?
Here is my code:
var Array2 = [ "1", "2", "3", "4", "5" ];
var Array1 = ["1","2","3"];
var result = [];
for(var i = 0; i < Array1.length ; i++){
var x = Array1[i];
var check = false;
for( var y = 0; y < Array2.length; y++){
if(x == Array2[y]){
check = true;
}
}
if(!check){
result.push(x);
}
}
console.log(result);