Created
April 5, 2020 01:36
-
-
Save leo-bianchi/13314beb795b7597004c7665dbdab6dd to your computer and use it in GitHub Desktop.
Simple Array Sum (HackerRank)
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
// 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