Hi Everyone,
My code for the Diff Two Array Algorithm challenge works for each of the test cases that are listed, but yet they all show a red X’s. I’ve also tested my code locally and it works fine. I can’t understand why it keeps failing, my results are correct each time. Maybe I’m missing something.
Any suggestions?
My code:
var diff = [];
function diffArray(arr1, arr2) {
var length = Math.max(arr1.length, arr2.length);
if (!(arr1.length === 0 || arr2.length === 0)) {
for (i=0;i<length;i++) {
if (arr1.indexOf(arr2[i]) < 0 && arr2[i] !== undefined) {
diff.push(arr2[i]);
}
if (arr2.indexOf(arr1[i]) < 0 && arr1[i] !== undefined) {
diff.push(arr1[i]);
}
}
}
if (arr1.length === 0) {
for (i=0;i<arr2.length;i++) {
diff.push(arr2[i]);
}
}
if (arr2.length === 0) {
for (i=0;i<arr1.length;i++) {
diff.push(arr1[i]);
}
}
return diff;
}
diffArray([“diorite”, “andesite”, “grass”, “dirt”, “pink wool”, “dead shrub”], [“diorite”, “andesite”, “grass”, “dirt”, “dead shrub”]);