Review my code for (Diff Two Arrays)

It is different from the other solutions and somewhat easier to understand in my humble opinion —

function diffArray(arr1, arr2) {
    const newArr = [];
    for(const element of arr1) {
        if(!arr2.includes(element)) {
            newArr.push(element);
        }
    }

    for(const element of arr2) {
        if(!arr1.includes(element)) {
            newArr.push(element);
        }
    }

    return newArr;
}

Since each for loop looks 95% similar, see if you could create a helper function that you can call passing the two arrays and performing the necessary checks and pushes.

2 Likes

Ok I think I see what you mean, like this … :thinking:

function diffArray(arr1, arr2) {
    const newArr = [];

    findDiff(arr1, arr2);
    findDiff(arr2, arr1);

    function findDiff(arrA, arrB) {
        for(const element of arrA) {    // check every element of arrA ...
            if(!arrB.includes(element)) {   // if that element not found in arrB add it to newArr
                newArr.push(element);
            }
        }
    }

    return newArr;
}