Sum All Numbers in a Range - Halfway there

Tell us what’s happening:
Detailed in comments below but basically I can get two of the four test cases to pass by changing the comparison operator but not all four. I feel like the math adds up either way but I can’t understand why it won’t fully pass.

  **Your code so far**

function sumAll(arr) {

let count = 0;
arr.sort((a, b) => a - b);

/*
//This lets the last two tests (5, 10 and 10, 5) but not the first two tests (1, 4 and 4, 1)
for(let i = 0; i < arr[1]; i++)
{
  count += i;
}
*/

////This lets the first two tests (1, 4 and 4, 1) but not the last two tests (5, 10 and 10, 5)
for(let i = 0; i <= arr[1]; i++)
{
  count += i;
}
//console.log(count);
return count;
}

sumAll([1, 4]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0

Challenge: Sum All Numbers in a Range

Link to the challenge:

Hi @foxesarezuper !

Welcome to the forum!

So you are close.

You need to reexamine this part.

Why are you starting at 0 every time?

Instead you should start with the first number in the array.

Hopefully, that was a big enough hint :grinning:

1 Like

Thank you! I knew it would be a simple fix but I just couldn’t quite put my finger on it! Of course, it makes sense now :laughing:

1 Like

Better use for(let i=arr[0]; i<=arr[1]; i++) . It gives the answer you want.

The answer was already marked as solved. There is no need to hand out the exact answer, especially after the OP has figured out how to fix their code with a good hint.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.