Forked from anonymous/bonfire-sum-all-numbers-in-a-range?solution=function%20sumAll(arr)%20%7B%0A%20%20var%20valA%20%3D%20arr%5B0%5D%3B%0A%20%20var%20valB%20%3D%20arr%5B1%5D%3B%0A%20%20%0A%20%20var%20max%20%3D%20Math.max(valA%2CvalB)%3B%0A%20%20var%20min%20%3D%20Math.min(
Created
November 11, 2015 08:37
-
-
Save tokoiwesley/6451c7da19612a67527f to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/tokoiwesley 's solution for Bonfire: Sum All Numbers in a Range
This file contains 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
// Bonfire: Sum All Numbers in a Range | |
// Author: @tokoiwesley | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sum-all-numbers-in-a-range?solution=function%20sumAll(arr)%20%7B%0A%20%20var%20valA%20%3D%20arr%5B0%5D%3B%0A%20%20var%20valB%20%3D%20arr%5B1%5D%3B%0A%20%20%0A%20%20var%20max%20%3D%20Math.max(valA%2CvalB)%3B%0A%20%20var%20min%20%3D%20Math.min(valA%2CvalB)%3B%0A%20%20%0A%20%20var%20newArr%20%3D%20%5B%5D%3B%0A%20%20%0A%20%20for%20(i%3Dmin%3B%20i%20%3C%3D%20max%3B%20i%2B%2B)%20%7B%0A%20%20%20%20newArr.push(i)%3B%0A%20%20%7D%0A%20%20%20%0A%20%20return%20newArr.reduce(function(previousValue%2C%20currentValue%2C%20index%2C%20array)%7B%0A%20%20%20%20return%20previousValue%20%2B%20currentValue%3B%0A%20%20%7D)%3B%0A%7D%0A%0AsumAll(%5B1%2C%204%5D)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function sumAll(arr) { | |
var valA = arr[0]; | |
var valB = arr[1]; | |
var max = Math.max(valA,valB); | |
var min = Math.min(valA,valB); | |
var newArr = []; | |
for (i=min; i <= max; i++) { | |
newArr.push(i); | |
} | |
return newArr.reduce(function(previousValue, currentValue, index, array){ | |
return previousValue + currentValue; | |
}); | |
} | |
sumAll([1, 4]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment