Sum All Numbers in a Range

This is the first algorithm of the Intermediate Algorithm Scripting series. In this function, we’ll pass you an array of two numbers. The goal is to return the sum of those two numbers and all numbers between them.

Let’s tell the tale of two chickeneggs (they have the face of a chicken but have the body of an egg). We’ll view them as an array of two numbers:

The elegance that is the chickenegg

Now you know that the values that come between 4 and 7 are:

And when you add 4 + 5 + 6 + 7, you get:

Another chickenegg… or the number 22. The array that the function is given must be in numerical order. The first value in the array should be the smallest number and the second value should be the largest number of the two. But be mindful that the smallest number may not be the first value.

Is 7 less than 4? Don’t bother asking a chickenegg this. They’ll just stand there and do their arms like above (even if you ask why is their body so round).

It helps to sort the array in numerical order first (you can use the javascript sort function with a compare function or a if-else if- else statement):

After you create you made sure that the smallest value is the first value, now it is time to create an empty array. So instead of an array of just 4 and 7, we can add an array that includes all the numbers between 4 and 7. Let’s look at our beloved empty chickenegg:

Look at him. He has no idea what the hell is going on.

All we need to do is to iterate all numbers between 4 and 7 (be sure to include 4 and 7 also).

Mr. For Loop hating his job as usual. Who likes to repeat the same thing over and over again.

After populating our empty array, it should be full by now:

Poor thing. He looks like a swollen rock now.

Now of course you could have used a variable called “total” or something and add index to the total and there you solved the function. But for this we are doing this extra step so we can understand the javascript function called reduce:

In a Reduce method, you pretty much reduce the “numbers” collection into the “total” variable. Let’s visually view this visual of how reduce works:

The hands of terror that is the reduce method

Reduce grabs all the values and turns it into:

One Value.

The End.

Well not really. Here is a better visual of how it works. Let’s start over:

In a reduce method, you have an accumulator, a current value, a current index, an array, and a return value.

The array is [4, 5, 6 ,7].

The current value will be the first item (index zero)in the array, 4:

The first chickenegg has no idea what is going on.

The accumulator (or total) will be zero as nothing has been added. Now it’s time for the next item in the array, 5:

The second chickenegg is looks quite confused now.

The accumulator or total is now 4 as 0+4 = 4. The current value now moves to the next item in the array, 6:

The third chicken egg is having a ball.

The accumulator is now 9 as 0+(4+5) = 9. I’m sure you are seeing the pattern now. Once 6 is added:

The last chickenegg is scared shitless.

Once 7 is added, you get:

And that is your return value.