Last active
April 8, 2021 13:19
-
-
Save joshfarrant/36d4fc734a5fa0f6ec438b3079341546 to your computer and use it in GitHub Desktop.
Code Kata Solutions - Sum of Positive
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 runningTotal = 0; | |
for (let i = 0; i < arr.length; i++) { | |
const value = arr[i]; | |
if (value > 0) { | |
runningTotal += value; | |
} | |
} | |
return runningTotal; | |
}; |
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 runningTotal = 0; | |
for (const value of arr) { | |
if (value > 0) { | |
runningTotal += value; | |
} | |
} | |
return runningTotal; | |
}; |
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 => ( | |
arr.reduce((runningTotal, value) => { | |
if (value > 0) { | |
return runningTotal + value; | |
} | |
return runningTotal; | |
}, 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
const positiveSum = arr => { | |
return arr | |
.filter(x => x > 0) | |
.reduce((a, c) => a + c, 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
const sum = (n1, n2) => n1 + n2; | |
const isPositive = num => num > 0; | |
const positiveSum = arr => arr | |
.filter(isPositive) | |
.reduce(sum, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment