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. πŸ‘