Just exploring my culture. I have grasped the concept of the reduce principle
var sumAll = function(...nums: number[]):void{
var sum = nums.reduce((a, b) => a + b , 0);
document.write("sum: " + sum + "<br/>");
}
sumAll(1,2,3,4,5);
The result is 15 because we start at 0.
and 1+2= 3 in the first call -> 1+2 are replaced by 3
3+3=6 in the second call 3 -> 3 +3 are replaced by 6
6+4 = 10 in the third call -> 6+4 are replaced by 10
10+5 = 15 in the fourth call -> 10 + 5 are replaced by the final result 15
It all adds up.
but what happens when I do
var sumAll = function(...nums: number[]):void{
var sum = nums.reduce((a, b, c) => a + b +c , 0);
document.write("sum: " + sum + "<br/>");
}
sumAll(1,2,3,4,5);
the result is 25 but I don't understand why... I assumed
first call: 1+2+3 = 6 (1+2+3 are replaced by 6)
second call 6+4+5 = 15 ( 6 +4 + 5 are replaced by 15 and it's the final result)
but why does the result give 25?
Thanks in advance;)