Sum Numbers in a Range - JavaScript Solution & Walkthrough
(01/21) Learn how to solve coding challenges using FreeCodeCamp's curriculum.
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 return10
because sum of all the numbers between 1 and 4 (both inclusive) is10
.
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!
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.