JAVASCRIPT | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JAVASCRIPT

What is the output of the following 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)); please whats the output?

23rd Apr 2020, 11:35 AM
Chidera Alakwe
Chidera Alakwe - avatar
4 Answers
+ 1
What output did you got?
23rd Apr 2020, 11:48 AM
Abhay
Abhay - avatar
+ 1
There is code playground to check output
23rd Apr 2020, 11:48 AM
Abhay
Abhay - avatar
+ 1
12
23rd Apr 2020, 12:13 PM
Alexander Lebedev
Alexander Lebedev - avatar
0
Actually you overcomplicated a bit. Try to use "reduce" like: return nums.filter(n => n % 2 == 0).reduce((n1,n2) => n1+n2);
23rd Apr 2020, 7:31 PM
Michal
Michal - avatar