Warning Probably don’t want to read if you haven’t started the Algorithms yet
Here is my Code for the Symmetric Difference exercise. It passes all the tests but I feel like it’s overly complicated could use a critical code review to point out what I should have seen.
Object.defineProperty(Array.prototype, 'removeItem', {
configurable: true,
writable: true,
value: function removeItem (index) {
'use strict'
var length = this.length;
if (index != length - 1) {
this[index] = this[length-1];
}
this.pop();
return this
}
});
function filter_diff(outputArray, inputArray){
return inputArray.filter((val)=> {
//remove any duplicates in inputArray
while(inputArray.indexOf(val) != inputArray.lastIndexOf(val)){
inputArray.removeItem(inputArray.lastIndexOf(val));
}
let foundItem = outputArray.indexOf(val);
if(foundItem === -1){
return true;
}else{
outputArray.removeItem(foundItem);
return false;
}
});
};
function sym(...args) {
var ansArray = [];
ansArray = args.reduce((acc,arr)=>{
let filteredArray = filter_diff(acc, arr);
return [...acc, ...filteredArray];
},[]);
return ansArray;
}
console.log(sym([1, 2, 3, 3], [5, 2, 1, 4]));