Diff of Two Arrays -- JavaScript -- Free Code Camp

preview_player
Показать описание

**Diff of Two Arrays** -- Finding the symmetric difference or the difference between two arrays. This function uses our good friends `filter()`, `concat()`, `slice()`, and `indexOf()`.
Рекомендации по теме
Комментарии
Автор

The following works for me:

function diffArray(arr1, arr2) {
var copy1 = arr1;
var copy2 = arr2;
for(i=0; i<copy1.length; i++){
for(j=0; j<copy2.length; j++){
if(copy1[i] == copy2[j]){
copy1.splice(i, 1);
copy2.splice(j, 1);
i-=1;
j-=1;
}
}
}
var new_Arr = copy1.concat(copy2);
return new_Arr;
}

elonwu
Автор

Thanks for this Stephen. I couldn't work out how to do this challenge using all the methods that FreeCodeCamp suggested. However, thankfully I did complete the challenge using the include method that you introduced in a previous video:

function diffArray(arr1, arr2) {
var firstArr = arr1.filter(function(letter) {
return !arr2.includes(letter);
});
var secondArr = arr2.filter(function(letter) {
return !arr1.includes(letter);
});
return firstArr.concat(secondArr);
}

supersteve
Автор

If the first array contains a duplicate value, and that value is not present in the second array, it will still filter it out.

young
Автор

your videos are amazing what books do you suggest? what are the best resources to learn javascript?

glendonphilippbaculio
visit shbcf.ru