-Enigmos-

guineaPig.js

Feb 19th, 2022 (edited)
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function guineaPig(input) {
  2.     let index = 0;
  3.     let food = Number(input[index]) * 1000;
  4.     index++;
  5.     let hay = Number(input[index]) * 1000;
  6.     index++;
  7.     let cover = Number(input[index]) * 1000;
  8.     index++;
  9.     let weight = Number(input[index]) * 1000;
  10.     index++;
  11.     let dayCounter = 0;
  12.     let goodsEnough = true;
  13.  
  14.     while (dayCounter < 30) {
  15.         dayCounter++;
  16.         if (food - 300 < 0) {
  17.             goodsEnough = false;
  18.             console.log("Merry must go to the pet store!");
  19.             break;
  20.         } else {
  21.             food -= 300;
  22.         }
  23.  
  24.         if (dayCounter % 2 === 0) {
  25.             let hayNeeded = food * 0.05;
  26.  
  27.             if (hay - hayNeeded < 0) {
  28.                 goodsEnough = false;
  29.                 console.log("Merry must go to the pet store!");
  30.                 break;
  31.             } else {
  32.                 hay -= hayNeeded;
  33.             }
  34.         }
  35.  
  36.         if (dayCounter % 3 === 0) {
  37.             let coverNeeded = weight / 3;
  38.            
  39.             if (cover - coverNeeded < 0) {
  40.                 goodsEnough = false;
  41.                 console.log("Merry must go to the pet store!");
  42.                 break;
  43.             } else {
  44.                 cover -= coverNeeded;
  45.             }
  46.         }
  47.     }
  48.     if (goodsEnough) {
  49.         console.log(`Everything is fine! Puppy is happy! Food: ${(food / 1000).toFixed(2)}, Hay: ${(hay / 1000).toFixed(2)}, Cover: ${(cover / 1000).toFixed(2)}.`);
  50.     }
  51. }
  52.  
  53. guineaPig(["10",
  54.     "5",
  55.     "5.2",
  56.     "1"]);
  57. guineaPig(["1",
  58.     "1.5",
  59.     "3",
  60.     "1.5"
  61. ]);
  62. guineaPig(["9",
  63.     "5",
  64.     "5.2",
  65.     "1"]);
Add Comment
Please, Sign In to add comment