#100DaysOfCode Day 10: Reduce
Reduce
Today is dedicated to my little friend Reduce. Reduce and I have a love-hate relationship. At first, I despised being reduced. It confused me. It made absolutely no sense. But then over time, I began to see the magic of Reduce and how powerful it is. Now I have trouble not using reduce since it’s so versatile. There is usually a better, more direct method that can be used in most cases.

Reduce is an array iterator method. Reduce will take in an array as an input and output what is known as an ‘accumulator’ (acc). It’s magic lies within this accumulator. This accumulator can be any data type, not just an array. That means we can output an integer, string, boolean, array, or object! Super useful.
In the example below, we have an arrayOfNumbers. We define a new variable assigned to the reduce method. The reduce uses the arrayOfNumbers and takes in 2 parameters; acc and currentNumber. We know what the ‘acc’ is. So what is the 2nd parameter? This parameter is a semantic representation of the current index in the array that is being evaluated. Since we know we are working on an array of numbers, I am going to call this variable currentNumber.
We then move onto the logic inside the reduce. Every single number in the arrayOfNumbers will be evaluated through this logic. Our currentNumber will only be pushed into the accumulator if it’s remainder is strictly equal to 1, making it an odd number. Once the reduce reaches the end of the arrayOfNumbers, it will stop and return that output to the AllOddNumbers variable.
We now have all odd numbers from the original array ~ magic!