// My question
I have a code like this and I understand a bit about reducing () but I still don’t understand how to set the specific variable in this case ‘’ a ‘’ and ‘’ b ‘’, what role do they have? How do these two variables add up the total numbers in my array?
// My code
const mytest = arr => arr.reduce((a,b) => a + b, 0);
Reduce callback takes (in this case) two paramaters a and b, where a is the accumulator and b is the current value.
Naming could be anything you want.
You can think of it as an for loop with a variable outside (a) that you assign with += in each iteration (b)
In this case running the reducer,
a = 0
b = 1
a = 1
b = 2
a = 3
b = 3
a = 6
b = 4
The 0 provided is the initial value set for the accumulator (a), which in this case is a starting value of 0.
If you write a 10 there instead it would return 20.
What a perfect answer, I understand, thanks