What is the output of the following code? function magic (...nums) { let sum = 0; nums.filter(n=>n%2==0).map(el=>sum+=El);} | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the output of the following code? function magic (...nums) { let sum = 0; nums.filter(n=>n%2==0).map(el=>sum+=El);}

27th Feb 2019, 10:24 AM
Nwankwo Obioha Chigozie
Nwankwo Obioha Chigozie - avatar
16 Answers
+ 13
the answer is 12, it because with this "nums.filter(n=>n%2==0)" all the argumnets that are mod by 2 should be equal to 0 and with "map(el=>sum+=El" the all result return by the "nums.filter(n=>n%2==0)" will be added to each other. that's how i understand it, i am not really good at speaking english hope you get my point. :D
23rd Jun 2019, 9:14 AM
Nathaniel Baleña
Nathaniel Baleña - avatar
+ 8
Referencing to Nathaniel Baleña, it means that with ""map(el=>sum+=El", it will filter out the numbers that we get from the original array. We should get 2, 4 and 6. Then, simply add 2, 4 and 6 together to get 12.
18th Nov 2020, 3:22 PM
Jason Chew
Jason Chew - avatar
+ 5
12
13th Apr 2019, 5:05 AM
Andri Hry
Andri Hry - avatar
+ 4
I want to understand how and why the result is 12, I have failed to understand some one please help me
26th Mar 2020, 7:54 AM
Kalaluka Katungu
Kalaluka Katungu - avatar
+ 3
answer is 12
10th Feb 2020, 8:32 AM
Pranjal Singh
Pranjal Singh - avatar
+ 2
How
27th Feb 2019, 10:27 AM
Nwankwo Obioha Chigozie
Nwankwo Obioha Chigozie - avatar
+ 2
right, the answer is: 12
18th Mar 2020, 7:04 AM
Md. jahidul islam
Md. jahidul islam - avatar
+ 1
This should be 12
25th Jul 2020, 4:04 PM
Ekundayo Timileyin
0
12
2nd Jul 2020, 8:50 PM
haitham hasan
haitham hasan - avatar
0
the answer is 12
30th Nov 2020, 2:26 AM
Youssef EL Khiyari
Youssef EL Khiyari - avatar
0
answer is 12
3rd Dec 2020, 7:29 PM
El Maslohi Hassan
0
12
7th Dec 2021, 11:53 AM
Md. Rakibul Islam
Md. Rakibul Islam - avatar
0
answer is 12
18th Jun 2022, 10:51 AM
Emmanuel kipsang
0
12
17th Sep 2022, 9:29 AM
Frank Osei Afful
0
answer is12
20th Oct 2022, 8:04 AM
mahla faridani
0
What will be the output? (Try it out. It’s tricky.) const nums = [1,2,3,4,5]; let output = nums.filter(n => n%2); console.log(output);
23rd Feb 2024, 8:49 AM
HARUN ROSHID
HARUN ROSHID - avatar