$(function(){
var x = new Array("a", "b", "c", "d", "e");
var y = new Array("a", "b", "3", "d", "e");
var str = "";
for (var i = 0; i < x.length; i++) {
for (var j = 0; j < y.length; j++) {
if (x[i] == y[j]) {
break;
} else {
//Check if reach the last element in the array 2
//If yes, then display that element in array 1 b/c not in array 2
if (y[j] == y.length - 1) {
str += x[i];
}
}
}
}
console.log(x[i]);
});
x[i] is returning undefined which should actually display the different value the is not found in both compared arrays. How do i get the value from x[i]? What am i doing wrong?
Change
if (y[j] == y.length - 1)toif (j == y.length - 1)so that you are doing a check on the current iterator position instead of y array value.And change
console.log(x[i]);toconsole.log(str);so that you output thestrvariable you set in the loop