SHARE
TWEET

Untitled

a guest Oct 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top