 # Sum All Numbers in a Range - I think I'm close

The idea of this code is as follows:

is the second element or the first one bigger?

After deciding on such, add both elements to the global var sum and then decrease the bigger element by one and increase the smaller by one.

Do this until the difference between them is just one - since this is when you know all numbers between between the smaller and bigger elements of the array have been taken into account. Add all of these into sum as the loop goes through.

However, debug shows output is 5 when it should be 10 for [1,4] so that means 2 and 3 are not being counted into the summation. Is this an issue with the while loop I defined? Is the use of Math.ab fine?

``````
function sumAll(arr) {

let first = arr;
let final = arr;
let sum = 0;

if (first>final) {

while ( (Math.abs(first-final)) >1) {

sum = sum + first + final;
first = first - 1;
final = final + 1;
}

} else if (final>first) {

while ( (Math.abs(first-final)) >1) {

sum = sum + first + final;
first = first + 1;
final = final - 1;

}

}
console.log(sum);

return sum;
}

sumAll([1, 4]);

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36`.

Your while in the else if doesn’t execute when first is 2 and final is 3.

So, the idea is, if the first element is smaller

say 5,10 then math.abs(firstElement - lastElement) will give 5 despite 5 - 10 = -5

if the first element is bigger

say 10,5 then math.abs(firstElement - lastElement) will also give 5 as 10 - 5 = 5

The use of it is to measure the distance between both elements and STOP when we know that all integers between both elements have been used i.e. they have been added to the total sum

Some kind of logic issue. After the first pass first = 2 and final = 3. Then on the second pass if(first>final) is false and else if (final>first) is true but the while fails so nothing further happens.
I think you’re getting the sum of first and final but the values of the numbers between them aren’t getting added in. Given [1,4] the function should execute something like 1+2+3+4 = 10 instead of 1+4 = 5.

I think given [1,4] it should execute
sum += 1 + 4 then loop again
sum += 2 + 3 then return sum.

Your while isn’t executing when first = 2 and final = 3 because Math.abs(first-final) is no longer > 1 so the final two numbers in between aren’t getting added to sum. I think you need the loop to execute one more time.

I hope this helps.

1 Like

Excellent, I get it now thank you. Any suggestions on what I can do in order to make the while loop stop at the point where the elements in the middle are next to each other i.e. the difference is one so the summation acts as required. A pointer/hint would be sufficient, not a solution, many thanks

I’m not sure if the way I did it is good but it works. I used a few logical ANDs and !=. I also had a test case for when the difference is one. My code is 23 lines. Someday it might get refactored.

Could you elaborate further on what code you used as I’m now at a loss on how to do this

find lowest, find highest, calculate difference

now you have to use just one for loop in which you use i to add to lowest and difference to end the loop

one more hint: assign lowest to total before the loop starts.

And now, see if you can sort it out by yourself. Good luck! 1 Like

Why did I make it so complicated lol? I had this weird idea that you had to start counting up by one from the smallest element and down from one from the biggest, what a joke ha. Thanks all!

``````function sumAll(arr) {

let total = 0;

if (arr<arr) {

for (let i = arr; i<=arr ; i++) {

total = total + i;

}

} else {

for (let i = arr; i<=arr; i++) {

total = total + i;

}