The following code detects if in a given array arr there are two items whose sum is equal to the value given in the variable sum. To address this problem the complementary values (sum-item) are stored into a Set.
Although I know the arrow functions and how functional some() function works, the syntax is obscure to me and I struggle to understand.
const arr = [3,6,7];
const sum = 9;
const findSum = (arr,sum) =>
arr.some((set => n =>
set.has(n) || !set.add(sum-n)) (new Set));
console.log(findSum(arr,sum));
I can't understand the (new Set) inside the some function (is a sort of IIFE? ) and how n is initialized to an item of arr, in my understanding the code set would initialized to an item of arr.
Someone can explain me how does it work?