DEV Community

Cover image for Codewar day 1
G33kNoob
G33kNoob

Posted on

2

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay