i was wondering if somebody could help me on my diff function. its supposed to find the diff between 2 inputs. the function takes in 2 arrays so if array 1 has.. "word1", "word2" and array 2 has "word1", "word2", and "word3", it should just return "word3". it works for the most part, but with the certain input below, it doesn't work.
function diff(a1, a2) {
if (a1.length > a2.length
|| a1.length == a2.length) {
return [""];
}
a1 = a1.slice(0);
a2 = a2.slice(0);
for (i = 0; i < a1.length; i++) {
for (k = 0; k < a2.length; k++) {
if (a1[i] == a2[k]) {
a1.splice(i, 1);
a2.splice(k, 1);
}
}
}
for (j = 0; j < a2.length; j++) {
for (p = 0; p < a1.length; p++) {
if (a2[j] == a1[p]) {
a2.splice(p, 1);
a1.splice(j, 1);
}
}
}
a1 = a1.concat(a2);
return a1;
}
var s1 = ["one", "two", "three", "four",];
var s2 = ["one", "two", "three", "four", "five"];
document.write(diff(s1, s2));
the answer should just return "five", but its returning "four", "four", "five". help would be much appreciated. im super stuck. thanks a bunch!