Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. const conditionalSum = function(values, condition) {
  2.  
  3. let odd = 0;
  4.  
  5. for (let i = 0; i < values.length; i++){
  6. if (values[i] % 2 === 0 && condition === "even"){
  7. odd += values[i];
  8.  
  9. } else if (values[i] % 2 !== 0 && condition === "odd"){
  10. odd += values[i];
  11. }
  12. }
  13. return odd;
  14. }
  15.  
  16. console.log(conditionalSum([1, 2, 3, 4, 5], "even"));
  17. console.log(conditionalSum([1, 2, 3, 4, 5], "odd"));
  18. console.log(conditionalSum([13, 88, 12, 44, 99], "even"));
  19. console.log(conditionalSum([], "odd"));
  20.  
  21.  
  22. // outputs: 6 9 144 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement