DEV Community

Mohammed Awad
Mohammed Awad

Posted on

Sum All Numbers in a Range

DESCRIPTION:

We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first.

Examples

sumAll([1, 4]) `should return 10`.

sumAll([10, 5]) `should return 45`.
Enter fullscreen mode Exit fullscreen mode

My approach for solving this problem:

  • find the min and max value in the array.
  • fill the array with range from min to max.
  • if not return sliced str with num length.
  • get the summation of the array with reduce().

My solution:

function sumAll(arr) {
  const minNumber = Math.min(...arr);
  const maxNumber = Math.max(...arr);
  return new Array(maxNumber - minNumber + 1)
  .fill()
  .map((num,index)=> minNumber+index)
  .reduce((perv,num) => perv + num)
}

sumAll([1, 4]);
Enter fullscreen mode Exit fullscreen mode

Any tips or edit are most welcome. share it with me on the comments. Thanks for being here!

Follow Muhmmad Awd on

If you have any questions or feedback, please feel free to contact me at

Top comments (22)

Collapse
ย 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข โ€ข Edited

My approach, based on the fact that the sum from 0โ†’n is equal to (n/2)(n+1):

const sumAll =
  (arr, [a,b]=[...arr].sort((c,d)=>c-d))=>(b*b-a*a+b+a)/2
Enter fullscreen mode Exit fullscreen mode

(Could be made shorter by not copying the original array, but I wanted to avoid mutation)

Collapse
ย 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข โ€ข Edited

With mutation (and more code golf):

const sumAll=(a,[b,c]=a.sort((d,e)=>d-e))=>(c*c-b*b+c+b)/2
Enter fullscreen mode Exit fullscreen mode
Collapse
ย 
xmohammedawad profile image
Mohammed Awad โ€ข

thank you, Relly for your interest!

Collapse
ย 
donnywi profile image
donnywi โ€ข

This works fast too when you pass in:
sumAll(7, 80000)

function sumAll(arr) {
    return ( (arr[0]+arr[1]) * (Math.abs(arr[0]-arr[1])+1) ) /2 ;
}

Enter fullscreen mode Exit fullscreen mode
Collapse
ย 
xmohammedawad profile image
Mohammed Awad โ€ข

I'm suck at Math; I hope to be become good like that someday.

Collapse
ย 
donnywi profile image
donnywi โ€ข

I think you're awesome at Javascript, and I need to learn how to use fill(), map(), and reduce() properly. I'm new at Javascript, so your posts are very inspiring.

Thread Thread
ย 
xmohammedawad profile image
Mohammed Awad โ€ข

Relly, glad to hear that. you can check freecodecamp.com to learn more

Collapse
ย 
syeo66 profile image
Red Ochsenbein (he/him) โ€ข

Try and be amazed:

function sumAll(arr) {
  const minNumber = Math.min(...arr);
  const maxNumber = Math.max(...arr);
  return (maxNumber*(maxNumber+1)/2) - ((minNumber-1)*minNumber/2);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
ย 
xmohammedawad profile image
Mohammed Awad โ€ข

this is a pattern ?

Collapse
ย 
donnywi profile image
donnywi โ€ข

I belive that's just a basic math equation.

Thread Thread
ย 
xmohammedawad profile image
Mohammed Awad โ€ข

Yeah, but I mean is it design pattern.

Thread Thread
ย 
syeo66 profile image
Red Ochsenbein (he/him) โ€ข

Nah. It's math. The basic idea is that you can calculate the sum of all integers from 1 to n using n*(n+1)/2

Thread Thread
ย 
xmohammedawad profile image
Mohammed Awad โ€ข

I need to focus on math and learn how use it because better with BigO

Thread Thread
ย 
syeo66 profile image
Red Ochsenbein (he/him) โ€ข

I'm not very good at math. I just happen to know some of the more usual stuff like exactly this one.

Collapse
ย 
link2twenty profile image
Andrew Bone โ€ข
function sumAll(arr) {
  const [a, l] = arr.sort((a, b) => a - b);

  return (l - a + 1) * (a + l) / 2;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
ย 
xmohammedawad profile image
Mohammed Awad โ€ข

Any tips or edit are most welcome. share it with me on the comments. Thanks for being here!

Collapse
ย 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข โ€ข Edited

Another one (no mutation):

const sumAll = ([m,n],[a,b]=m>n?[n,m]:[m,n])=>(b*b-a*a+b+a)/2
Enter fullscreen mode Exit fullscreen mode
Collapse
ย 
xmohammedawad profile image
Mohammed Awad โ€ข

great but it's seemed little complex, right? I more into simple solutions

Collapse
ย 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข โ€ข Edited

It's interesting you would say that, as your solution is actually the most complex (timewise) here at O(N). The simplest I can now think of is a small reworking of @donnywi solution (which in itself is a slight mathematical simplification of others here):

const sumAll = ([a, b]) => (a+b) * (Math.abs(a-b)+1) / 2 
Enter fullscreen mode Exit fullscreen mode
Thread Thread
ย 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข

I suspect the way I wrote my code in posts here may have made it look more complex than it is, as it doesn't really explain itself. Sorry, my brain often tends to work in terse, code-golf like code.

Thread Thread
ย 
xmohammedawad profile image
Mohammed Awad โ€ข

yes, I know my code was the worst, but it easier to read than yours, I should study the math section again, because I didn't Relly use Math methods expect min, max and random.

Thread Thread
ย 
donnywi profile image
donnywi โ€ข

This is a good use of the arrow function. ๐Ÿ‘