Created
September 4, 2021 05:44
-
-
Save madeleine68/9810e02211b1129c370c7e33245d3d6d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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