Solving "Sum All Numbers in a Range" / freeCodeCamp Algorithm Challenges
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-06-30 19:03:40

---
title: Solving "Sum All Numbers in a Range" / freeCodeCamp Algorithm Challenges
published: true
description: My guide, notes, and solution to freeCodeCamp's basic algorithm challenge, "Sum All Numbers in a Range" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-017-sum-all
series: Solving freeCodeCamp's Algorithm Challenges
---

![Sum All Numbers in a Range](//images.ctfassets.net/yzwpq1epaq68/1TfA6607Hwpcc8wJ4Y4oSq/60388df77ae8507bb4040fc4e365db1d/ss-017.png)

Let's solve freeCodeCamp's intermediate algorithm scripting challenge, 'Sum All Numbers in a Range'.

### Starter Code
```javascript
function sumAll(arr) {
  return 1;
}

sumAll([1, 4]);
```

### Instructions

We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first.

For example, `sumAll([4,1])` should return `10` because sum of all the numbers between 1 and 4 (both inclusive) is `10`.


### Tests

```
sumAll([1, 4]) should return a number.
sumAll([1, 4]) should return 10.
sumAll([4, 1]) should return 10.
sumAll([5, 10]) should return 45.
sumAll([10, 5]) should return 45.
```

---

# Our Approach

**Read** everything first. **Read** the instructions clearly, **read** the starter code we're given, and **read** the tests and understand what has to be returned.

* The function takes one arguments, `arr` being an array.
* We need to sum the numbers plus all the numbers between the two.
* We need to return a number.

Now that we understand what we are given and what we want to output, let's see how we can solve this.

If you've completed the 'Functional Programming' section of freeCodeCamp, you would have been to some new array methods, `reduce()`, `filter()`, `map()`, and `sort()`.

Since we have to figure out the numbers in between the two given to us in `arr`, I would start by running `sort()` on `arr`, so `arr[0]` would be the smallest number.

[MDN Documentation: sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort "MDN sort()")

`arr.sort((a,b) => a - b);`

I will also declare a new empty array to add all the numbers in between into.

`let fullArr = [];`

In order to get all the numbers in between, I will create a for loop, and push each value into our `fullArr`.

```
 for (let i = arr[0]; i <= arr[1]; i++) {
    fullArr.push(i);
 }
```

If we `sort()` as above then run our for loop, `fullArr` on `sumAll([1,4])` should be `[1, 2, 3, 4]`.

We now have all the numbers we need in an array. We just have to figure out how to add them all up.

Enter another new-er array method, `reduce()`.

*'The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.'*

That sounds like what we want, a single output value.

[MDN Documentation: reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce "MDN Docs reduce")

**How to use reduce?**

```
const array1 = [1, 2, 3, 4];

const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
```

Ensure you are returning a value!

So putting it all together with some pseudocode -

```
function sumAll(arr) {
	create new empty array, fullArr
	run sort method on array to arrange [small, big]
	for loop on sorted arr
		push each value into fullArr
		
	fullArr, run reduce method, creating one single output integer value
	return value
}
```


---

# Our Solution

>
> **SPOILER: ANSWER BELOW**
>

```javascript
function sumAll(arr) {
  let fullArr = [];
  arr.sort((a,b) => a - b);
  for (let i = arr[0]; i <= arr[1]; i++) {
    fullArr.push(i);
  }
  let sum = fullArr.reduce((acc, currVal) => {return acc + currVal}, 0);
  return sum;
}
```

---


### Links & Resources

['Sum All Numbers in a Range' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range "Intermediate Algorithm Scripting: Sum All Numbers in a Range on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/017---sum-all.js  "GitHub Solution")

Thank you for reading!