I was wondering what was the use of the 0 in this function.
Was it for the default value of a ?
In this way of view, if i multiply instead of additioning, it always return 0
Also, the “args” given is a standard method in javascript ? Can i change it to any value ?
Also, we are only incrementing a, b, what is the procedure behind it to add more values ? (additional question)
I have some troubles to understand it
Your code so far
const sum = (...args) => args.reduce((a, b) => a + b, 0);
console.log(sum(1,2))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Challenge: ES6 - Use the Rest Parameter with Function Parameters
a is the final result you will get from reduce 0 at the end is the initial value of the a b represents every item in the array
So when you use reduce it will take every b one by one
and perform operation e.g.( + ) with the final value assume a
and records the result in a
In this case it is 0 you can change initial value to anything.
Just to be clear, a is the “accumulator”. It is the result being passed from the previous iteration. It is not the final result. The final result will be returned but reduce.
Often instead off “a” and “b”, these are called “a” and “c” or “acc” and “cur” for “accumulator” and “current”. But you can call them whatever you want, and if there are more descriptive names, then that works.
Another message of help, you truly are a good person, and i mean it !
I wish i knew this website way sooner.
Another way to think through all of this, i am eager to start my projects and use what i learn here