Petar_Matev

Untitled

Feb 1st, 2024
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // For-Loop - More Exercises
  2. // 11. Odd / Even Position
  3.  
  4. function oddEvenPosition(input) {
  5.   let num = Number(input[0]);
  6.  
  7.   let oddsum = 0;
  8.   let oddmin = Math.min();
  9.   let oddmax = Math.max();
  10.   let evensum = 0;
  11.   let evenmin = Math.min();
  12.   let evenmax = Math.max();
  13.  
  14.   for (i = 1; i <= num; i++) {
  15.     let currentNum = parseFloat(input[i]);
  16.  
  17.     if (i % 2 === 0) {
  18.       evensum += currentNum;
  19.  
  20.       if (currentNum < evenmin) {
  21.         evenmin = currentNum;
  22.       }
  23.       if (currentNum > evenmax) {
  24.         evenmax = currentNum
  25.       }
  26.  
  27.     } else {
  28.       oddsum += currentNum;
  29.  
  30.       if (currentNum < oddmin) {
  31.         oddmin = currentNum;
  32.       }
  33.       if (currentNum > oddmax) {
  34.         oddmax = currentNum
  35.       }
  36.     }
  37.   }
  38.  
  39.   console.log(`OddSum=${oddsum.toFixed(2)},`);
  40.  
  41.   if (oddmin != Math.min()) {
  42.     console.log(`OddMin=${oddmin.toFixed(2)},`);
  43.   } else {
  44.     console.log(`OddMin=No,`);
  45.   }
  46.  
  47.   if (oddmax != Math.max()) {
  48.     console.log(`OddMax=${oddmax.toFixed(2)},`);
  49.   } else {
  50.     console.log(`OddMax=No,`);
  51.   }
  52.  
  53.   console.log(`EvenSum=${evensum.toFixed(2)},`);
  54.  
  55.  if (evenmin != Math.min()) {
  56.     console.log(`EvenMin=${evenmin.toFixed(2)},`);
  57.   } else {
  58.     console.log(`EvenMin=No,`);
  59.   }
  60.  
  61.   if (evenmax != Math.max()) {
  62.     console.log(`EvenMax=${evenmax.toFixed(2)}`);
  63.   } else {
  64.     console.log(`EvenMax=No`);
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment