Created
April 13, 2022 14:27
-
-
Save jcolebot/8b5eb45c36092b7b9fc6245f56a7a0aa to your computer and use it in GitHub Desktop.
freeCodeCamp: Sum All Numbers in a Range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. | |
| // For example, sumAll([4,1]) should return 10 because sum of all the numbers between 1 and 4 (both inclusive) is 10. | |
| function sumAll(arr) { | |
| let total = 0; | |
| if(arr[0] < arr[1]) { | |
| for(let i = arr[0]; i <= arr[1]; i++) { | |
| total = total + i; | |
| } | |
| } | |
| if(arr[0] > arr[1]) { | |
| for(let i = arr[1]; i <= arr[0]; i++) { | |
| total = total + i; | |
| } | |
| } | |
| return total; | |
| } | |
| sumAll([1, 4]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment