View on GitHub

marymafa.github.io


layout : post title: Reduce function and recursive date: 2018-01-12 —

Reduce

The reduce function executes a provided function for each value of the array (from left-to-right. If you can use the reduce function to spit out an average then you can use it any way you want. For example, you could double the total, or half each number before adding them together, or use an if statement inside the reducer to only add numbers that are greater than 10. My point is that the Reduce Method In JavaScript​ gives you a mini CodePen where you can write whatever logic you want. It will repeat the logic for each amount in the array and then return a single value.

Finding an Average with the Reduce Method In JavaScript​:

Instead of logging the sum, you could divide the sum by the length of the array before you return a final value. The way to do this is by taking advantage of the other arguments in the reduce method. The first of those arguments is the index. Much like a for-loop, the index refers to the number of times the reducer has looped over the array.

Recursive

occurs when a thing is defined in terms of itself or of its type. is used in a variety of disciplines ranging from linguistics to logic. Or relating to or involving a program or routine of which a part requires the application of the whole, so that its explicit interpretation requires in general many successive executions.