Skip to content

Instantly share code, notes, and snippets.

@madeleine68
Created September 4, 2021 05:44
Show Gist options
  • Save madeleine68/9810e02211b1129c370c7e33245d3d6d to your computer and use it in GitHub Desktop.
Save madeleine68/9810e02211b1129c370c7e33245d3d6d to your computer and use it in GitHub Desktop.
let conditionalSum = function(values, condition) {
var sum = 0;
values.forEach( function(value) {
if(condition == "even" && value % 2 === 0) {
sum += value;
} else if (condition == 'odd' && value % 2 !== 0) {
sum +=value;
}
});
return sum;
};
console.log(conditionalSum([1, 2, 3, 4, 5], "even"));
console.log(conditionalSum([1, 2, 3, 4, 5], "odd"));
console.log(conditionalSum([13, 88, 12, 44, 99], "even"));
console.log(conditionalSum([], "odd"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment