DEV Community

Discussion on: Solving "Sum All Numbers in a Range" / freeCodeCamp Algorithm Challenges

Collapse
 
brandontingvgc profile image
丁友亮

Hi Viren,

Thanks for sharing! Just want to ask why not just add numbers in for loop. Like below

  let sum = 0;
  for (let i = arr[0]; i <= arr[1]; i++) {
    sum+= i
  }
  return sum
Enter fullscreen mode Exit fullscreen mode

Is there any problem with this method? Thanks.

Collapse
 
namhle profile image
Nam Hoang Le

No. There is no problem in your methodology. But I think you assumed that arr[0] <= arr[1] ? Have your read description carefully?