Can anyone please help explain the science behind the output of this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can anyone please help explain the science behind the output of this code?

function add(arr) { var a = arr.reduce(function(p, c) { return p + c; }) return a; } function multiply(arr, func) { var doubles = arr.map(function(n) { return n * n; }); return func(doubles); } var m = multiply([1,2,3,4], add); console.log(m); // output 30

18th Aug 2019, 7:28 PM
eMBee
eMBee - avatar
8 Answers
+ 3
The output is 30. The multiply function returns an array where each element is squared: [1,4,9,16]. The add function returns the sum of all array elements: 1+4+9+16 = 30.
18th Aug 2019, 7:40 PM
Michael
Michael - avatar
+ 3
Oh, thank you ~ swim ~
18th Aug 2019, 8:44 PM
eMBee
eMBee - avatar
+ 2
I wish you were correct Michael but your answer is not what SoloLearn gave as answer in the challenges section
18th Aug 2019, 7:43 PM
eMBee
eMBee - avatar
18th Aug 2019, 7:48 PM
Michael
Michael - avatar
+ 2
Alright, thanks Michael and ~ swim ~
18th Aug 2019, 8:32 PM
eMBee
eMBee - avatar
+ 2
Honestly, ~ swim ~, SoloLearn are meant to make you a platinum Moderator
18th Aug 2019, 8:51 PM
eMBee
eMBee - avatar
+ 2
Your level of knowledge ~ swim ~
18th Aug 2019, 9:33 PM
eMBee
eMBee - avatar
+ 1
Michael and ~ swim ~, you can review this question here to see if I'm wrong or SoloLearn https://www.sololearn.com/post/137749/?ref=app
18th Aug 2019, 8:39 PM
eMBee
eMBee - avatar