Flaw in solution 1 - add the range

Tell us what’s happening:
Describe your issue in detail here.

The issue with the first hint solution (solution 1) for the add the range numbers challenge is that if the same numbers are passed as arguments to the sumAll function, it simply returns one of the number, doesn’t add the two up as expected or atleast instructed in the challenge.

My fix: I placed an if statement to check when the maxNum and minNum are equal, then return their sum like this - maxNum+minNum

Works well!

Hope this helps someone.

  **Your code so far**

function sumAll(arr){
  let maxNum = Math.max(arr[0],arr[1]);
  let minNum = Math.min(arr[0],arr[1]);
  let sumBetween = 0;
  for( i = maxNum; i >= minNum; i--){
    sumBetween +=i
  if(maxNum === minNum){
    return maxNum + minNum
  return sumBetween


  **Your browser information:**

User Agent is: Mozilla/5.0 (Linux; Android 11; Nokia G10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36

Challenge: Sum All Numbers in a Range

Link to the challenge:

The task is to sum a range of numbers. So if the range is [4,4], then that’s just the number 4. If it’s [4,5], then that’s 4 and 5. If it’s [4,6], then that’s 4 and 5 and 6. And so on.

The description is slightly confusing in this respect, but it’s assuming the numbers are different: the example given is [4, 1], which is 1 and 2 and 3 and 4, ie the two given numbers 4 and 1, and then the two numbers in-between (2 and 3).

The if statement not necessary you’re saying?

The challenge is “sum a range of numbers”.

So if you have the number 1 as the start of the range and 4 as the end of the range, the numbers you have to add up are 1, 2, 3 and 4. That’s a range from 1 to 4. The sum of the numbers from 1 to 4, inclusive, is 10. 1 + 2 + 3 + 4.

If you have the number 4 as the start of the range and 4 as the end of the range, the range is from 4 to 4. The sum of the numbers from 4 to 4 is just 4, it can’t be anything else. Your code says the sum of the numbers between 4 and 4 (inclusive) is 8, which is incorrect.

As I say, I think the description here is slightly confusing, I understand why you posted that code, but the description is written as if the two numbers are different

1 Like