Zheng Fu 5dd27f88e4 simplifying the mathematical solution for Sum All Numbers in a Range (#35007)
* simplifying the mathematical solution

We don't need to sort the two number array, using Math.abs() we can get the count of the numbers in range and calculate the sum.

* Remove previously relevant info regarding sort()

* Clarify code for better readability

* change let to const according to review

* added missing semicolon
2019-03-27 22:26:59 +05:30

6.3 KiB

title
title
Sum All Numbers in a Range

:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:

:checkered_flag: Problem Explanation:

You need to create a program that will take an array of two numbers who are not necessarily in order, and then add not just those numbers but any numbers in between. For example, [3,1] will be the same as 1+2+3 and not just 3+1

:speech_balloon: Hint: 1

Use Math.max() to find the maximum value of two numbers.

try to solve the problem now

:speech_balloon: Hint: 2

Use Math.min() to find the minimum value of two numbers.

try to solve the problem now

:speech_balloon: Hint: 3

Remember to that you must add all the numbers in between so this would require a way to get those numbers.

try to solve the problem now

Spoiler Alert!

warning sign

Solution ahead!

:beginner: Basic Code Solution:

function sumAll(arr) {
    var max = Math.max(arr[0], arr[1]);
    var min = Math.min(arr[0], arr[1]);
    var temp = 0;
    for (var i=min; i <= max; i++){
        temp += i;
    }
  return(temp);
}

sumAll([1, 4]);

:rocket: Run Code

Code Explanation:

  • First create a variable to store the max number between two.
  • The same as before for the Smallest number.
  • We create a temporary variable to add the numbers.

Since the numbers might not be always in order, using max() and min() will help organize.

:sunflower: Intermediate Code Solution:

const sumAll = (arr) => {
  // Buckle up everything to one!
  const startNum = arr[0];
  const endNum = arr[1];
  
  // Get the count of numbers between the two numbers by subtracting them and add 1 to the absolute value.
  // ex. There are |1-4| + 1 = 4, (1, 2, 3, 4), 4 numbers between 1 and 4.
  const numCount = Math.abs(startNum - endNum) + 1
  
  // Using Arithmetic Progression summing formula
  const sum = (startNum + endNum) * numCount / 2;
  return sum;
};

:rocket: Run Code

Code Explanation:

  • The formula for calculating the sum of a continuous range is "(startNum + endNum) * numCount / 2".
  • arr[0] and arr[1] can either be startNum or endNum, order doesn't matter.
  • We can get the count of numbers in range by "Math.abs(arr[0] - arr[1]) + 1".
  • Applying the formula by plugging in the numbers.

:rotating_light: Advanced Code Solution:

function sumAll(arr) {
    var sum = 0;
    for (var i = Math.min(...arr); i <= Math.max(...arr); i++){
        sum += i;
    }
  return sum;
}

sumAll([1, 4]);

:rocket: Run Code

Code Explanation:

  • Creating a variable sum to store the sum of the elements.
  • Starting iteration of the loop from min element of given array and stopping when it reaches the max element.
  • Using a spread operator (...arr) allows passing the actual array to the function instead of one-by-one elements.

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light: