Last active
April 9, 2019 19:05
-
-
Save pratyushcrd/32559d68ac2db9fc407e08065664e322 to your computer and use it in GitHub Desktop.
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
const positiveSum = arr => { | |
let sum = 0 | |
let i = 0 | |
let ii = arr.length | |
for (i = 0; i < ii; ++i) { | |
if (arr[i] > 0) { | |
sum += arr[i] | |
} | |
} | |
} | |
// A more elegant approach | |
const positiveSum = arr => arr | |
.filter(num => num > 0) | |
.reduce((a, b) => a + b) | |
// Even shorter | |
const positiveSum = arr => arr | |
.reduce((acc, v) => Math.max(acc, acc + v), 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment