Created
October 24, 2017 08:07
-
-
Save farskid/5169e210fef568dacd07341c3b43ad4c to your computer and use it in GitHub Desktop.
Sum of an array of integers in Javascript
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
function sumOfIntArray(array) { | |
if (!array.length) { | |
return 0; | |
} | |
return array.pop() + sumOfIntArray(array); | |
} |
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
function sumOfIntArray(array) { | |
return array.reduce(function(total, current) { return total + current; }, 0); | |
} |
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
function sumOfIntArray(array) { | |
var sum = 0; | |
while (array.length) { | |
sum += array.pop(); | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment