Spocoman

07. Hotel Room

Dec 20th, 2021 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Решение с if-else:
  2.  
  3. function hotelRoom(input) {
  4.     let mount = input[0];
  5.     let nights = Number(input[1]);
  6.     let apartment = 0;
  7.     let studio = 0;
  8.  
  9.     if (mount === "May" || mount === "October") {
  10.         apartment = 65;
  11.         studio = 50;
  12.         if (nights > 7 && nights <= 14) {
  13.             studio *= 0.95;
  14.         } else if (nights > 14) {
  15.             studio *= 0.7;
  16.         }
  17.     } else if (mount === "June" || mount === "September") {
  18.         apartment = 68.7;
  19.         studio = 75.2;
  20.         if (nights > 14) {
  21.             studio *= 0.8;
  22.         }
  23.     } else if (mount === "July" || mount === "August") {
  24.         apartment = 77;
  25.         studio = 76;
  26.     }
  27.  
  28.     if (nights > 14) {
  29.         apartment *= 0.9;
  30.     }
  31.  
  32.     console.log(`Apartment: ${(apartment * nights).toFixed(2)} lv.`);
  33.     console.log(`Studio: ${(studio * nights).toFixed(2)} lv.`);
  34. }
  35.  
  36. Решение със switch:
  37.  
  38. function hotelRoom(input) {
  39.     let mount = input[0];
  40.     let nights = Number(input[1]);
  41.     let apartment = 0;
  42.     let studio = 0;
  43.  
  44.     switch (mount) {
  45.         case "May":
  46.         case "October":
  47.             apartment = 65;
  48.             studio = 50;
  49.             if (nights > 7 && nights <= 14) {
  50.                 studio *= 0.95;
  51.             } else if (nights > 14) {
  52.                 studio *= 0.7;
  53.             }
  54.             break;
  55.  
  56.         case "June":
  57.         case "September":
  58.             apartment = 68.7;
  59.             studio = 75.2;
  60.             if (nights > 14) {
  61.                 studio *= 0.8;
  62.             }
  63.             break;
  64.  
  65.         case "July":
  66.         case "August":
  67.             apartment = 77;
  68.             studio = 76;
  69.             break;
  70.     }
  71.  
  72.     if (nights > 14) {
  73.         apartment *= 0.9;
  74.     }
  75.  
  76.     console.log(`Apartment: ${(apartment * nights).toFixed(2)} lv.`);
  77.     console.log(`Studio: ${(studio * nights).toFixed(2)} lv.`);
  78. }
  79.  
  80. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
  81.  
  82. function hotelRoom(input) {
  83.     let mount = input[0];
  84.     let nights = Number(input[1]);
  85.  
  86.     let apartment = (mount === "May" || mount === "October") ? 65 * (nights > 14 ? 0.9 : 1) :
  87.                     (mount === "June" || mount === "September") ? 68.7 * (nights > 14 ? 0.9 : 1) :
  88.                     (mount === "July" || mount === "August") ? 77 * (nights > 14 ? 0.9 : 1) : 0;
  89.  
  90.     let studio = (mount === "May" || mount === "October") ? 50 * (nights > 7 && nights <= 14 ? 0.95 : nights > 14 ? 0.7 : 1) :
  91.                  (mount === "June" || mount === "September") ? 75.2 * (nights > 14 ? 0.8 : 1) :
  92.                  (mount === "July" || mount === "August") ? 76 : 0;
  93.  
  94.     console.log(`Apartment: ${(apartment * nights).toFixed(2)} lv.`);
  95.     console.log(`Studio: ${(studio * nights).toFixed(2)} lv.`);
  96. }
  97.  
Add Comment
Please, Sign In to add comment