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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more