DEV Community

Cover image for Codewar day 1
G33kNoob
G33kNoob

Posted on

Codewar day 1

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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;
};
Enter fullscreen mode Exit fullscreen mode

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));
}
Enter fullscreen mode Exit fullscreen mode

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)