You need to create a program that will take an array of two numbers who are not necessarily in order, and then add not just those numbers but any numbers in between. For example, [3,1] will be the same as 1+2+3 and not just 3+1
Hints
Hint 1
Use Math.max() to find the maximum value of two numbers.
Hint 2
Use Math.min() to find the minimum value of two numbers.
Hint 3
Remember to that you must add all the numbers in between so this would require a way to get those numbers.
Solutions
Solution 1 (Click to Show/Hide)
function sumAll(arr) {
let max = Math.max(arr[0], arr[1]);
let min = Math.min(arr[0], arr[1]);
let sumBetween = 0;
for (let i = min; i <= max; i++) {
sumBetween += i;
}
return sumBetween;
}
sumAll([1, 4]);
Code Explanation
First create a variable to store the max number between two.
The same as before for the Smallest number.
We create a accumulator variable to add the numbers.
Since the numbers might not be always in order, using max() and min() will help organize.
const sumAll = arr => {
// Buckle up everything to one!
const startNum = arr[0];
const endNum = arr[1];
// Get the count of numbers between the two numbers by subtracting them and add 1 to the absolute value.
// ex. There are |1-4| + 1 = 4, (1, 2, 3, 4), 4 numbers between 1 and 4.
const numCount = Math.abs(startNum - endNum) + 1;
// Using Arithmetic Progression summing formula
const sum = ((startNum + endNum) * numCount) / 2;
return sum;
};
Code Explanation
The formula for calculating the sum of a continuous range is â(startNum + endNum) * numCount / 2â.
arr[0] and arr[1] can either be startNum or endNum, order doesnât matter.
We can get the count of numbers in range by âMath.abs(arr[0] - arr[1]) + 1â.
function sumAll(arr) {
const [first, last] = [...arr].sort((a, b) => a - b);
return first !== last
? first + sumAll([first + 1, last])
: first;
}
sumAll([1, 4]);
That is the challenge, here are solutions, totally different. If they put reduce as a helpful link then that means it can be used to solve the problem. We just happened to have solutions that donât use it here. These solutions are originally from multiple campers and not an official hard answer to the challenge, you are free to come up with your own solution as long as it works regardless of what you use.
@Rafase282: I thought that you were working hand in hand with those designing the hints. Hence my surprise. But I get your point.
@P1xt: I really like your solution but I struggle to understand your code coming after the return. Would you mind explaining this line step by step for me?
@samuelpath I made most if not all the hints you will see on the on the wiki, the ones on the main site were not done by me. However, FCC is planing on integrating the hints from the wiki into the challenges themselves in the future.
I wouldnât call myself a master at JavaScript, there are many areas that i donât know or could improve, not directly being on JS. Regardless, thanks!
function sumAll(arr) {
arr = arr.sort((a, b) => a - b);
var count = arr[1] - arr[0] + 1;
return Array
// create empty array of length = 'count'
.apply(null, new Array(count))
// populate with values from input array range
.map((num, ind) => arr[0] + ind)
// sum all the values in the range
.reduce(function (accum, curval) {
return accum + curval;
});
}
I used the below after reading about Arithmetic Progression. Interested in anyoneâs thoughts. It ignores the use of any array related work and just sticks to the math aspect of the question.
function sumAll (arr) {
const min = Math.min(...arr);
const max = Math.max(...arr);
return (((max - min + 1) * (min + max)) / (2));
}
Hey, yeah well Iâve pretty much the same as P1xt already wrote above, which in turn pretty much resembles your code. The solution is pretty elegant. Itâs actually just a transformation of basic arithmetic math (GauĂ). Donât get me wrong, I also spent quite some time until I figured it out. But the more thrilling insight for me is how valuable good old math is when it comes to solving problems with code! So good work!