today i challenge my self to solve minimum 1 codewar challenge everyday. ok lets start the first day!!
the problem :
Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list a, which are present in list b.
arrayDiff([1,2],[1]) == [2]
If a value is present in b, all of its occurrences must be removed from the other:
arrayDiff([1,2,2,2,3],[2]) == [1,3]
my solving :
const arrayDiff = (a, b) => {
let arr = a;
let filterarray = b.forEach((item, i) => {
let n = item;
let resultfilter = arr.filter(value => {
return value !== n;
});
arr = resultfilter;
return resultfilter;
});
return arr;
};
i know the solving is not good as other. but its worked haha :D
after solving this, i read the most popular answer , and this is the result :
function array_diff(a, b) {
return a.filter(e => !b.includes(e));
}
im never hear "includes array function", now i must learn again to get more refference.
i think array.prototype.includes is very fun to usefull, hope i can use it next time
Top comments (0)