Advertisement
Spocoman

11. Odd / Even Position

Dec 26th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function oddEvenPosition(input) {
  2.     let n = parseInt(input[0]);
  3.     let oddMin = Number.MAX_VALUE;
  4.     let oddMax = -Number.MAX_VALUE;
  5.     let oddSum = 0;
  6.     let evenMin = Number.MAX_VALUE;
  7.     let evenMax = -Number.MAX_VALUE;
  8.     let evenSum = 0;
  9.  
  10.     for (let i = 1; i <= n; i++) {
  11.         let currentNum = parseFloat(input[i]);
  12.         if (i % 2 === 1) {
  13.             oddSum += currentNum;
  14.             if (currentNum < oddMin) {
  15.                 oddMin = currentNum;
  16.             }
  17.  
  18.             if (currentNum > oddMax) {
  19.                 oddMax = currentNum;
  20.             }
  21.         } else {
  22.             evenSum += currentNum;
  23.             if (currentNum < evenMin) {
  24.                 evenMin = currentNum;
  25.             }
  26.             if (currentNum > evenMax) {
  27.                 evenMax = currentNum;
  28.             }
  29.         }
  30.     }
  31.  
  32.     if (n === 0) {
  33.         console.log("OddSum=0.00,\nOddMin=No,\nOddMax=No,");
  34.     } else {
  35.         console.log(`OddSum=${oddSum.toFixed(2)},\nOddMin=${oddMin.toFixed(2)},\nOddMax=${oddMax.toFixed(2)},`);
  36.     }
  37.  
  38.     if (n <= 1) {
  39.         console.log("EvenSum=0.00,\nEvenMin=No,\nEvenMax=No");
  40.     } else {
  41.         console.log(`EvenSum=${evenSum.toFixed(2)},\nEvenMin=${evenMin.toFixed(2)},\nEvenMax=${evenMax.toFixed(2)}`);
  42.     }
  43.  
  44. }
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement