Skip to main content

Command Palette

Search for a command to run...

Sum Numbers in a Range - JavaScript Solution & Walkthrough

(01/21) Learn how to solve coding challenges using FreeCodeCamp's curriculum.

Updated
โ€ข3 min read
Sum Numbers in a Range - JavaScript Solution & Walkthrough
B

I am a full stack developer passionate about web accessibility. When I'm not behind the screens, you'll likely find me reading or going for a 5K run.

01/21 Sum Numbers in a Range

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.

function sumAll(arr) {
  return 1;
}

sumAll([1, 4]);

Credit: FreeCodeCamp.org

Understanding the Challenge

In this challenge, we are given two numbers in an array. Our tasks is to create a function that adds those two numbers plus all the numbers between them.

In the given example, we have sumAll([4,1]) This should return the sum of 1 + 2 + 3 + 4, which is 10. Another example might be sumAll([6,8]) This should return 21, that is 6 + 7 + 8.

Note that we're also informed "the lowest number will not always be the first number"

Now that we know what this challenge expects of us, let's go ahead and write some pseudocode that will guide us in writing the actual code.

Pseudocode

Sort the given array in ascending order. 

Create a new variable finalSum with an initial value of 0.

Create a loop initialized at the value of the smallest number
  For every iteration, add the current number i to finalSum
  keep the loop running as long as i <= highest number

Return finalSum

Solving the Challenge

First, we need to find a way of knowing which of the two numbers in the given array is the lowest and which is the highest. There are many ways of doing that. One way is to use the .sort() method.

With this method, we can sort the array in ascending order. That way, the lowest number will be the first number in the array and the highest number will be the second number.

let sorted = arr.sort((a, b) => a - b)

The code above returns a sorted version of arr in ascending order. Basically, we are using the .sort() method and we've given it an arrow function as a parameter.

What this function does is to compare the values in the given arr. It then returns a version of arr which has the lowest number as the first item and the highest number as the second.

You can learn more from this short article on .sort() method.

Next, we create a variable to hold the finalSum with an initial value of 0.

let finalSum = 0;

We can then use a for loop to find the sum of the two numbers and all numbers between them. Note that the lowest number is now at sorted[0] and the highest at sorted[1]

for (let num = sorted[0]; num <= sorted[1]; num++) {
  finalSum += num;
}

Finally, we return the value of finalSum.

return finalSum;

Our sumAll() function is now complete!

Final Solution

function sumAll(arr) {
  let sorted = arr.sort((a, b) => a - b);
  let finalSum = 0;

  for (let num = sorted[0]; num <= sorted[1]; num++) {
    finalSum += num;
  };

  Return finalSum;
};

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

You just cracked the first challenge in this series. Cheers and happy coding!

congratulations 001.gif

What do you think of this approach? Do you know of a more efficient approach to solve this? I would love to hear your feedback. Drop them in the comments section.

Let's connect on Twitter ๐Ÿ˜Ž


T

Great article, Benjamin! I like your approach, breaking down the problem and then tackling the subtasks individually. I enjoyed the article you referenced for the sort() method as well.

I look forward to reading more of your articles in this series.

1
B

The Overaged Developer Thanks for your feedback!

1
L
Luchi4y ago

I love this, Benjamin. I think you should do more of it.

I especially love the breakdown. Sometimes I jump into problem solving too quickly.

Definitely need to give pseudocode more attention and practice.

Good job you ๐Ÿ‘

1
B

Pseudocoding is useful. It helps you to formulate more efficient solutions.

JavaScript

Part 1 of 33

This JavaScript (JS) includes tutorials on various JS concepts. It also has solutions to common JS coding challenges.

Up next

Diff Two Arrays - JavaScript Solution & Walkthrough

(02/21) Learn how to solve coding challenges using FreeCodeCamp's curriculum.