Advertisement
Spocoman

10. Odd Even Sum

Dec 23rd, 2021 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function oddEvenSum(input) {
  2.     let n = parseInt(input[0]);
  3.     let evenSum = 0;
  4.     let oddSum = 0;
  5.  
  6.     for (let i = 1; i <= n; i++) {
  7.         let num = parseInt(input[i]);
  8.         if (i % 2 === 1) {
  9.             oddSum += num;
  10.         } else
  11.             evenSum += num;
  12.     }
  13.     if (oddSum === evenSum) {
  14.         console.log("Yes");
  15.         console.log(`Sum = ${oddSum}`);
  16.     } else {
  17.         console.log("No");
  18.         console.log(`Diff = ${Math.abs(oddSum - evenSum)}`);
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement