Spocoman

04. Fishing Boat

Dec 20th, 2021 (edited)
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fishingBoat(input){
  2.     let budget = Number(input[0]);
  3.     let season = input[1];
  4.     let people = Number(input[2]);
  5.     let sum = 0;
  6.  
  7.     switch (season) {
  8.         case "Spring":
  9.             sum = 3000;
  10.             break;
  11.         case "Summer":
  12.         case "Autumn":
  13.             sum = 4200;
  14.             break;
  15.         case "Winter":
  16.             sum = 2600;
  17.             break;
  18.     }
  19.  
  20.     if (people <= 6) {
  21.         sum *= 0.9;
  22.     } else if (people > 6 && people <= 11) {
  23.         sum *= 0.85;
  24.     } else {
  25.         sum *= 0.75;
  26.     }
  27.  
  28.     if (people % 2 === 0 && season !== "Autumn") {
  29.         sum *= 0.95;
  30.     }
  31.  
  32.     if (budget >= sum) {
  33.         console.log(`Yes! You have ${(budget - sum).toFixed(2)} leva left.`);
  34.     } else {
  35.         console.log(`Not enough money! You need ${(sum - budget).toFixed(2)} leva.`);
  36.     }
  37. }
  38.  
  39. РЕШЕНИЕ С IF ELSE ЛЕКО ТАРИКАТСКАТА:
  40.  
  41. function fishingBoat(input) {
  42.     let budget = Number(input[0]);
  43.     let season = input[1];
  44.     let people = Number(input[2]);
  45.     let sum = 4200;
  46.  
  47.     if (season === "Spring") {
  48.         sum = 3000;
  49.     } else if (season === "Winter") {
  50.         sum = 2600;
  51.     }
  52.  
  53.     if (people <= 6) {
  54.         sum *= 0.9;
  55.     } else if (people > 11) {
  56.         sum *= 0.75;
  57.     } else {
  58.         sum *= 0.85;
  59.     }
  60.  
  61.     if (people % 2 === 0 && season !== "Autumn") {
  62.         sum *= 0.95;
  63.     }
  64.  
  65.     if (budget >= sum) {
  66.         console.log(`Yes! You have ${(budget - sum).toFixed(2)} leva left.`);
  67.     } else {
  68.         console.log(`Not enough money! You need ${(sum - budget).toFixed(2)} leva.`);
  69.     }
  70. }
  71.  
  72. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
  73.  
  74. function FishingBoat(input) {
  75.     let budget = Number(input[0]);
  76.     let season = input[1];
  77.     let people = Number(input[2]);
  78.  
  79.     budget -= ((season === "Spring" ? 3000 : season === "Winter" ? 2600 : 4200) *
  80.         (people <= 6 ? 0.9 : people > 11 ? 0.75 : 0.85) *
  81.         (people % 2 === 0 && season !== "Autumn" ? 0.95 : 1));
  82.  
  83.     console.log(budget >= 0 ? `Yes! You have ${budget.toFixed(2)} leva left.` :
  84.         `Not enough money! You need ${Math.abs(budget).toFixed(2)} leva.`);
  85. }
  86.  
Add Comment
Please, Sign In to add comment