On JavaScript reduce method
When I watched a lesson on JavaScript reduce method and heard the instructor say it's one of the hardest, I simply shrugged - "meh"
That's because at first, I understood the basic mechanism. It reduces the values in the parameters to a single digit.
Simple right?
Nope!
A few JavaScript exercises later and I'm nearly smacking my head against the wall - "I can't seem to understand this reduce method anymore."
Thanks to web development simplified, I finally got the gist
So here's a simple function to use reduce
function reduceArr((){
})
This can accept 4 parameters. But for purposes of clarity, let's start with the commonly used.
2 parameters alone
So, function reduceArr ((total, iteration)
{
}, initalElement)
So, this method accepts a callback function and another parameter.
The last parameter is the starter "total"
That's, it's the element that will be the initial total in the first iteration.
Which is usually zero.
So, in the next iteration, the next element of the array ends up in the iteration parameter, and it adds to the total.
And it continues the loop until the end of the array.
So to finalize,
function reduceArr((total, iteration) => {
return total
},0)
reduceArr([1,2,3,4])
The end result should be 10
Watch Web simplified tutorial here
Comments
Post a Comment