JavaScript's Reduce
One of the functions I had no previous experience with before Flatiron, was the reduce method. While I had worked with find, filter, and map functions, reduce was foreign. Not only that, reduce seemed counterintuitive. The idea of the reduce method is to take a bunch of values and return a single value. That makes sense right? The syntax of the reduce method is more complicated. It takes 4 arguments and has 6 parameters.
The two non-argument parameters are:
Callback function - this is the function run on every element of the array
Index - this is the index of the array
The reducer's arguments:
Accumulator - this is the result of the callback function adding to itself. This value will add the callback function's last result to the accumulation of all previous results.
Current Value - this value is the latest value of the array to be added.
Initial Value - this can be changed to determine the exact starting location for the reducer
Array - this is the array that's being "reduced"
The basic idea is to do this: Array.Reduce(function(accumulator, current value, index, array) {
return accumulator + current value
}, initial value)
where the callback is the function that determines how the accumulator and current value is used. In the above example, the two are simply added, so a reduction of an array of the elements [1, 2, 3, 4] would result in 10, if no initial value was provided.