Advertisement
Spocoman

05. Game Of Intervals

Dec 25th, 2021 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function gameOfIntervals(input) {
  2.     let num = Number(input[0]);
  3.     let result = 0.00;
  4.     let num0To10 = 0;
  5.     let num10To20 = 0;
  6.     let num20To30 = 0;
  7.     let num30To40 = 0;
  8.     let num40To50 = 0;
  9.     let numInvalid = 0;
  10.  
  11.     for (let i = 1; i <= num; i++) {
  12.         let number = Number(input[i]);
  13.         if (number >= 0 && number < 10) {
  14.             result += number * 0.2;
  15.             num0To10++;
  16.         } else if (number >= 10 && number < 20) {
  17.             result += number * 0.3;
  18.             num10To20++;
  19.         } else if (number >= 20 && number < 30) {
  20.             result += number * 0.4;
  21.             num20To30++;
  22.         } else if (number >= 30 && number < 40) {
  23.             result += 50;
  24.             num30To40++;
  25.         } else if (number >= 40 && number <= 50) {
  26.             result += 100;
  27.             num40To50++;
  28.         } else{
  29.             result /= 2;
  30.             numInvalid++;
  31.         }
  32.     }
  33.  
  34.     console.log(`${result.toFixed(2)}`);
  35.     console.log(`From 0 to 9: ${(num0To10 / num * 100).toFixed(2)}%`);
  36.     console.log(`From 10 to 19: ${(num10To20 / num * 100).toFixed(2)}%`);
  37.     console.log(`From 20 to 29: ${(num20To30 / num * 100).toFixed(2)}%`);
  38.     console.log(`From 30 to 39: ${(num30To40 / num * 100).toFixed(2)}%`);
  39.     console.log(`From 40 to 50: ${(num40To50 / num * 100).toFixed(2)}%`);
  40.     console.log(`Invalid numbers: ${(numInvalid / num * 100).toFixed(2)}%`);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement