Sum All Numbers in a Range

A solution to Free Code Camp’s “Sum All Numbers in a Range” JavaScript challenge, in a Functional Programming style.

A friend of mine hosts meetups for the Free Code Camp, which describes itself as:

We’re an open source community of people who learn to code and help nonprofits.

He organises casual meetings in coffee shops to allow participants to work on their assignments in a nice environment and help each other. If you’re looking to learn programming I recommend finding a Free Code Camp group in your area.

Today I visited such a meeting and one of the Code Campers was working on a solution for one of the JavaScript problems in the Intermediate Algorithm Scripting section. The task was to write a function that takes an array of 2 numbers as an argument and returns the sum of all the numbers between them, including the 2 numbers themselves. For example, an input of [3, 1] should result in 6, because 1 + 2 + 3 = 6. As a hint, it’s recommended you read the documentation for Math.min(), Math.max() and Array.prototype.reduce().

His solution worked, it was clearly written and well structured. It involved defining two variables for the smaller and bigger number, for example by var smaller = Math.min(arguments[0], arguments[1]), then using those values in a “for loop” which pushed an element into an array, where the value would come from the index of the current iteration in the loop. Finally he would sum this array using a callback using a simple callback that adds up the values. This uses all of the functions mentioned in the hints.

For reference here is a similar solution by someone else, sorting the input instead of using min() and max().

The main place I saw for improvement was the for loop. If we could avoid it, we could elegantly chain all our logical steps into a single statement, clearly describing our function. Using some nice syntactic sugar from ES6, I was able to distil the algorithm to the following:

  1. function header expected by Free Code Camp’s tests, x is an array
  2. create a new array to hold our range (docs), note: we return the final summed result
  3. # elements will be (max - min + 1), the +1 is needed to include the upper value, e.g. 5..10 = (10 - 5 + 1) = (5 + 1) = 6
  4. lambda populates array with incrementing values starting from min (the array keys k already increment)
  5. reduce array by summing current number c into sum s
  6. Note: min() and max() expect several numbers as arguments, not a single array, ...x is how we spread x into arguments

I like this solution because I feel it better describes what we’re trying to make this function do and it is very concise without being unreadable. Several of the features it uses are only available in ES6 and it may be even better with ES7’s comprehensions, in fact I spent quite a bit of time searching for these features in JavaScript because they’re available in other languages like Python and Ruby.

Hopefully someone trying to write more concise and functional JavaScript code finds this post useful, or perhaps a Code Camper will stumble upon it and get inspired in their learning. 🙂

P.S. here’s an even shorter solution!