Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 2.37 KB

File metadata and controls

60 lines (46 loc) · 2.37 KB

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

Hint: 1

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

Hint: 2

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

Hint: 3

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

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

Code Solution 1:

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]);

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.

Code Solution 2:

function sumAll(arr) {
  // Buckle up everything to one!

  // Using ES6 arrow function (one-liner)
  var sortedArr = arr.sort((a,b) => a-b);
  var firstNum = arr[0];
  var lastNum = arr[1];
  // Using Arithmetic Progression summing formula
  // https://en.wikipedia.org/wiki/Arithmetic_progression#Sum
  var sum = (lastNum - firstNum + 1) * (firstNum + lastNum) / 2;
  return sum;
}

Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat: thanks @Rafase282 @abhisekp

NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)