JavaScript : Reduce Method (Lotto Edition)

In this post, I’ll be explaining the Reduce Method in JavaScript programming using the Feynman Technique and the lottery as my test sample. This post is part of the Feynman/20 hr challenge. For more info, click here.

The reduce method in JavaScript is associated with arrays. Just a reminder, arrays are a grouping of items that are listed together under one name. They can be numbers, objects, or strings (word/words).

The Reduce Method boils down all the values within an array into one single value, whether through adding, subtracting, or sorting. This can be done by your standard math (PEMDAS – parentheses, exponents, multiplication, division, addition, subtraction) or by using a boolean (true or false statement) to boil it all down.

Javascript Foundation Code:

const myLottoNum = [11, 18, 36, 44, 57, 64];

The code below is the reference we’ll be using for our samples, moving forward.

If we wanted to create a lucky number using the added value of these lotto numbers, we can…

  • Use a function to put together a list of instructions, then
  • In the instructions, use the reduce method to calculate the totals, like this:
const luckyNum = myLottoNum.reduce((acc, cur) => acc + cur,0);

The code above translates to: “my luckyNum is the value of what my lotto numbers are added together, starting with the value of “0”.

As for what “acc” and “cur” mean, I’ll need to explain the four arguments (the info that lives only in this list of instructions that involve the array, that is needed to make the magic happen.

What are acc, cur, i, and arr in JavaScript?

The Reduce Method works with four pieces of information, in the following order. Occasionally there will be one or two of these “argument” types that are left empty.

  1. acc: Accumulator (think rolling snowball).
  2. cur: current element in the array (what the present item is within the group).
  3. i: Current index in the array (where you are at).
  4. arr: The entire array.

Reduce Method Usage

When putting together the method, we always need the “acc” and the “cur” values. The “i” and the “arr” aren’t always used, which can be seen from our above sample code. We cannot exclude the first two because reduce cannot work without them, like not having air in a tire to make a car move.

As to the boolean (true/false) face of the reduce method, which is represented by the “if” in the code, it usually acts as the on/off switch for a code to either stop if the requested action is fulfilled or keep going. We can even use it to pick out a specific number in our array. Let’s use it to pick the largest number in our array above to choose as our mega ball.

const megaball = myLottoNum.reduce((acc, cur) => { 
if (acc > cur) return acc; 
else return cur;
}, myLottoNum[0]);

The above code translates to: “Go through myLottoNum. If the accumulated number is greater than the current number in the list, then keep the accumulated number, otherwise, swap it for the current number. Start at the very beginning of this group”.

Now please excuse me as I go buy some more lottery tickets. ๐Ÿค‘๐Ÿค‘๐Ÿค‘

For more on this, check out the links below:

Happy Coding!