Free Code Camp Bonfire: Sum All Numbers in a Range

jackLyons Free Code Camp Tags:
0

Alright, now that I have completed the first round of Front End projects, finally we are back into scripting! In this post I’ll show you how to solve the first intermediate bonfire problem where you need to sum all numbers in a range. For example, if your range is [1 – 5], you’ll need to add 1+2+3+4+5 together.

Some helpful links:

Here’s what you are given to start off with:

function sumAll(arr) {

  return arr;
}
sumAll([1, 4]);

 How to solve the problem:

Step 1: Set the Max and the Min.

I like this problem because the hints are actually really straightforward. Basically, if you’re going to iterate through from the smallest number up to the largest number then it makes sense (hopefully by now!) to set those two numbers as variables. This might sound easy enough to just write something like Math.max(arr), however that’s not going to cut it! Give it a go in your browser…. You should find that it returns NaN.

Why? Well, because we are passing in an Array as an argument which in fact is Not a Number! If you do a bit of digging around, you’ll soon find out about the following methods:

var min = Math.min.apply(0, arr);
var max = Math.max.apply(0, arr);

By using Apply, the method now accepts an array and it applies the array as parameters to the actual function, which, in this case, is the Math.max/min method. The 0 argument can be named whatever you like. For more information on the why behind it all, I referenced a really great blog post above which explains this concept in further detail.

Step 2: Create a loop starting from min, ending at max.

In our loop, we want to push through all the numbers from min to max into a new array. Once we have that array filled up we can sort it in ascending order. I used a simple for loop to get the job done.

var result = [];
  for (var i = minArr; i <= maxArr; i++) {
    result.push(i);

Step 3: Sort the array using reduce().

There’s not much more to this problem than sorting the array from min to max. In the hints section, you’re given a reference to the reduce method, which is worth looking over. Basically, the reduce() method reduces a set of numbers to a single number (like a sum). When you add all the numbers up, you’re left with the final result 🙂

function sumAll(arr) {
  var maxArr = Math.max.apply(0, arr);
  var minArr = Math.min.apply(0, arr);
  var result = [];
  for (var i = minArr; i <= maxArr; i++) {
    result.push(i);
  }
  return result.reduce(function (a, b) {
    return a + b;
  });
}

sumAll([1, 4]);

Answer = 10.

If you found this lesson helpful or have any questions please feel free to leave a comment below.


The Web Developers Bootcamp.

A n00b friendly guide to becoming a pro!

Learning to code can be seriously hard work. While I am oh so grateful for the opportunity to learn through Free Code Camp, I still find that learning to code alone and without proper guidance extremely exhausting.

Which is why I’ve started to substitute my education with an incredible online resource called the Web Developers Bootcamp courtesy of Udemy. This course has helped me brush off the cobwebs, unlearn my self-taught bad habits and gain a solid understanding of the true fundamentals of web development. It has helped me see why it is important to understand these JavaScript problems and how they apply to the real world.

This bootcamp will teach you how built beautiful sites with Bootstrap, add in functionality with JavaScript and write back end code with Node.js all in a series of short, sharp, no BS video modules. I seriously could not recommend it any higher. Check it out and let me know what you think 🙂

The Web Developers Bootcamp
The Web Developer Bootcamp.
The only course you need to learn web development – HTML, CSS, JS, Node, and More!

Sign up to receive up-to-date web tips, tricks and code hacks!