Skip to content

Instantly share code, notes, and snippets.

@leo-bianchi
Created April 5, 2020 01:36
Show Gist options
  • Save leo-bianchi/13314beb795b7597004c7665dbdab6dd to your computer and use it in GitHub Desktop.
Save leo-bianchi/13314beb795b7597004c7665dbdab6dd to your computer and use it in GitHub Desktop.
Simple Array Sum (HackerRank)
// Without Reduce
function simpleArraySum(ar) {
let total = 0;
for(let elem in ar) {
total += ar[elem]
}
return total;
}
// With Reduce
function simpleArraySum(ar) {
return ar.reduce(function(acc, current) {
return acc + current
}, 0)
}
// With Reduce and Arrow Function
function simpleArraySum(ar) {
return ar.reduce((acc, current) => acc + current, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment