In this freeCodeCamp algorithm, our function will take accept an array of two numbers as an argument. From there we will need to return the sum of those two numbers in addition to the sum of the numbers between them.

Requirements

  • 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

Psuedocode

Let's break down what steps we need to take to write our solution:

// Create a function that accepts an array as an argument
    
    // Create a variable to store the result
    
    // Sort the array from low to high
        // Return sorted array
        
    // Loop through sorted array 
    // initializing the loop at the lowest number in array
    // and ending loop at the largest number in array
        // Add each number in loop to result variable
    
    // Return result variable

Solving the Algorithm

Setting up the function

Let's start with the freeCodeCamp boilerplate that gives us our function and the array argument, arr:

function sumAll(arr) {
  return 1;
}

sumAll([1, 4]);

Create the variable to store the result

Here we need to create a variable that will hold the result that we will be returning at the end of the function:

function sumAll(arr) {

    // Initialize variable to hold result
    var res = 0;
    
    return 1;
}

sumAll([1, 4]);

Sort arr from low to high

Here we'll use the .sort() method that will allow us to sort arr from high to low. There are various other use cases for Array.sort() so definitely read up on them here.

For our use case, we can refer to this part of the documentation:

To compare numbers instead of strings, the compare function can simply subtract b from a. The following function will sort the array ascending (if it doesn't contain Infinity and NaN)

function sumAll(arr) {

	var res = 0;
	// Sort array low to high
	arr.sort((a,b) => {
		return a - b;
	});

	return 1;
}

sumAll([4, 1]);

Loop through arr and return res

We need to create loop through all numbers, starting with arr[0] and ending with arr[0]. By doing so, we can increment res after each pass of the loop which will ultimately sum all of the numbers in the range between arr[0] - arr[1]. Lastly, we'll just need to change our return statement to return res instead of returning 1 which is from the original function boilerplate.

function sumAll(arr) {

	var res = 0;
	// Sort array low to high
	arr.sort((a,b) => {
		return a - b;
	});
	
	for (var i = arr[0]; i <= arr[1]; i++) {
		
		res += i;

	}
	return res;
}

sumAll([4, 1]); // Returns 10

Try this the code snippet in your browser console.

Boom 💥

Awesome - hopefully this was a helpful walkthrough on the freeCodeCamp algorithm, 'Sum All Numbers in a Range'. Feel free to send me your suggestions or solutions and I'd be happy to feature them here!

Shoot me an email at tim@timwheeler.com with any questions and if you enjoyed this, stay tuned and subscribe below! 👇