When I first started learning JavaScript, I had some trouble grasping reduce(). I have heard of other people having trouble with it when they start...
For further actions, you may consider blocking this person and/or reporting abuse
That's why it's important use well named variables, imagine that your first approach with reduce was this way:
arr.reduce((accumulator, currentValue) => accumulator + currentValue)
;Much better, don't you think so? It's almost self explanatory...
I'm pretty sure I would still have found a way to be confused by it, hahaha
But I do agree that clear naming goes a long way to help understand code.
This is still confusing to someone who has never worked with
reduce()
. But then makes explaining it a bit more easy.This is a great post - really well written and clear; it removes all the confusion surrounding
reduce
. Thanks Pixie! :]Nice explanation, well done 👏
This was awesome, thanks for the article.
"I understand that a and b are supposed to be elements in the array, but how on earth does JavaScript know which one is which?"
I don't understand any of these questions you were asking when first analyzing reduce. First a and b were not elements in an array, but rather parameters, that is how they are figured out which are which. The person asking those questions, don't understand the difference between functions and arrays.
Well,
b
is an element of the array in the sense that it represents the element currently being processed in the array. Even MDN speaks of it that way (and evena
would be an element in the array for the first invocation of the callback function in that particular case.)At any rate, I think the root of our misunderstanding is that you seem to be taking my words as talking about their syntactic function (so to speak), while I'm talking about the value they represent and how
reduce()
deals with them.Thanks, sometimes I forget how to use the function and topic like this is so helpful
Hi, where would the semicolons be placed in example 2?
holy s**t!!! it's so clear now, thank you!!