Skip to content

Instantly share code, notes, and snippets.

@GeekaholicLin
Created August 30, 2016 04:53
Show Gist options
  • Save GeekaholicLin/855099f864388e8d679374959765c2fd to your computer and use it in GitHub Desktop.
Save GeekaholicLin/855099f864388e8d679374959765c2fd to your computer and use it in GitHub Desktop.
Sum All Numbers in a Range
function sumAll(arr) {
  var max = Math.max.apply(null,arr),
  /*How does Math.max.apply() work?See this answer in the Stackoverflow
  http://stackoverflow.com/questions/21255138/how-does-the-math-max-apply-work#answer-21255326
  */
      min = Math.min.apply(null,arr),
      count = max - min + 1,
      sum = ((max+min)*count/2);
 
  return sum;
}

sumAll([1, 4]);//test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment