Skip to content

Instantly share code, notes, and snippets.

@farskid
Created October 24, 2017 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farskid/5169e210fef568dacd07341c3b43ad4c to your computer and use it in GitHub Desktop.
Save farskid/5169e210fef568dacd07341c3b43ad4c to your computer and use it in GitHub Desktop.
Sum of an array of integers in Javascript
function sumOfIntArray(array) {
if (!array.length) {
return 0;
}
return array.pop() + sumOfIntArray(array);
}
function sumOfIntArray(array) {
return array.reduce(function(total, current) { return total + current; }, 0);
}
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