Please explain this code | SoloLearn: Learn to code for FREE!

0

Please explain this code

function magic(...nums) { let sum=0; nums.filter(n=>n%2==0).map(el=>sum+=el); return sum; } console.log(magic(1,2,3,4,5,6)); Output:12

5/10/2019 5:42:26 PM

Rucha

2 Answers

New Answer

+4

Ruhita magic method is passed 6 numbers (1,2,3,4,5,6) as arguments filter method take each argument at a time and checks if n%2==0 (i.e all even numbers) if the number is even it is mapped to sum by the map method which adds the number to previous value of sum sum starts with 0, filter will filter out even nos i.e 2,4,6 and sum of these three numbers is 12 sum += 2 (sum = 2) sum += 4 (sum = 6) sum += 6 (sum = 12)

0

What is el=> in that