const sum = (x, y, z) => {
const args = […args];
return args.reduce((a,b) => a + b, 0);
}
I understand that …args takes in a variable number of arguments. What I am getting hung up on is that suddenly we have two parameters, a and b in the return function statement. May someone please explain to me why that is? What exactly is going on with the inputs that we went from x, y, z (3 parameters) to variable number of arguments, and now there is a function that only has a and b (2 parameters). What happened to the 3rd parameter? I am not necessarily looking for the answer to the problem, rather the way this data is moving thru this system.
Thank you in advance for taking the time to read my question. Much appreciated.
So reduce method is a complex one but will try to explain you at my best here…
So as a method it takes 4 arguments in general but here you have 2 since you want the sum of your array of numbers: the a is the accumulator and the b is the total, the 0 it means that it starts from 0 index. So basically check what exactly is happening below:
I really appreciate this answer. You were thorough with your explanation and the visual was very helpful. I usually can figure out the problems on my own, so I suspected it was some sort of format for the reduce method. I could not find explanations about this even in the YouTube tutorials for this problem.
For others who see this posted question and want more examples, I found another YouTube explanation that supplements what VickyV posted for reduce method. Also, Kodiri looks like a fun way to practice: