Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
21st Apr 2019, 6:58 AM
Kartik
Kartik - avatar
+ 3
Can you explain this example step-by-step? const findSum = (arr, sum) => arr.some((set => n => set.has(n) || !set.add(sum - n))(new Set)); set is a new Set() n is arr element Since some() is used, every element of arr is checked until it is completed or if it gets a True value while executing. set.has(n) || !set.add(sum - n) First it checks whether the set has n or not and if it is true, then it will directly return true and stop. Then, it adds (sum - n) in the set and since ! is added, it will always return False. Now take this (given in the article) arr = [3,5,1,4] sum = 9 1st: set does not have 3 Add, set = 9-3 set = 6 2nd: set does not have 5 Add, set = 6, 9-5 set = 6, 4 3rd: set does not have 1 Add, set = 6, 4, 9-1 set = 6, 4, 8 4th: set does have 4 Returns True I am not good at explanations but hope that you will understand it...
21st Apr 2019, 6:23 AM
Kartik
Kartik - avatar
21st Apr 2019, 6:50 AM
Calviղ
Calviղ - avatar
+ 2
rudolph flash Glad I could help. 😄
21st Apr 2019, 7:26 AM
Kartik
Kartik - avatar