Skip to content

Instantly share code, notes, and snippets.

@joshfarrant
Last active April 8, 2021 13:19
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 joshfarrant/36d4fc734a5fa0f6ec438b3079341546 to your computer and use it in GitHub Desktop.
Save joshfarrant/36d4fc734a5fa0f6ec438b3079341546 to your computer and use it in GitHub Desktop.
Code Kata Solutions - Sum of Positive
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;
};
const positiveSum = arr => {
let runningTotal = 0;
for (const value of arr) {
if (value > 0) {
runningTotal += value;
}
}
return runningTotal;
};
const positiveSum = arr => (
arr.reduce((runningTotal, value) => {
if (value > 0) {
return runningTotal + value;
}
return runningTotal;
}, 0)
);
const positiveSum = arr => {
return arr
.filter(x => x > 0)
.reduce((a, c) => a + c, 0);
};
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