Spocoman

Santas Holiday

May 29th, 2022 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function santasHoliday(input) {
  2.     let days = Number(input[0]) - 1;
  3.     let type = input[1];
  4.     let rating = input[2];
  5.     let price = 0;
  6.  
  7.     if (days > 0) {
  8.         if (type == "room for one person") {
  9.             price = 18;
  10.         } else if (type == "apartment") {
  11.             price = 25;
  12.  
  13.             if (days < 10) {
  14.                 price *= 0.7;
  15.             } else if (days > 15) {
  16.                 price *= 0.5;
  17.             } else {
  18.                 price *= 0.65;
  19.             }
  20.         } else if (type == "president apartment") {
  21.             price = 35;
  22.  
  23.             if (days < 10) {
  24.                 price *= 0.9;
  25.             } else if (days > 15) {
  26.                 price *= 0.8;
  27.             } else {
  28.                 price *= 0.85;
  29.             }
  30.         }
  31.  
  32.         if (rating == "positive") {
  33.             price *= 1.25;
  34.         } else {
  35.             price *= 0.9;
  36.         }
  37.  
  38.         price *= days;
  39.     }
  40.  
  41.     console.log(`${price.toFixed(2)}`);
  42. }
  43.  
  44.  
  45. РЕШЕНИЕ С IF И ТЕРНАРЕН ОПЕРАТОР:
  46.  
  47. function santasHoliday(input) {
  48.     let days = Number(input[0]) - 1;
  49.     let type = input[1];
  50.     let rating = input[2];
  51.     let price = 0;
  52.  
  53.     if (days > 0) {
  54.         price = type == "room for one person" ? 18 : type == "apartment" ? 25 : 35;
  55.  
  56.         if (days < 10) {
  57.             price *= type == "apartment" ? 0.7 : type == "president apartment" ? 0.9 : 1;
  58.         } else if (days > 15) {
  59.             price *= type == "apartment" ? 0.5 : type == "president apartment" ? 0.8 : 1;
  60.         } else {
  61.             price *= type == "apartment" ? 0.65 : type == "president apartment" ? 0.85 : 1;
  62.         }
  63.  
  64.         price *= days * (rating == "positive" ? 1.25 : 0.9);
  65.     }
  66.     console.log(`${price.toFixed(2)}`);
  67. }
  68.  
  69.  
  70. РЕШЕНИЕ САМО С ТЕРНАРЕН ОПЕРАТОР:
  71.  
  72. function santasHoliday(input) {
  73.     let days = Number(input[0]) - 1;
  74.     let type = input[1];
  75.     let rating = input[2];
  76.  
  77.     let price = days > 0 ? ((type == "room for one person" ? 18 : type == "apartment" ? 25 : 35) *
  78.             (days < 10 ? (type == "apartment" ? 0.7 : type == "president apartment" ? 0.9 : 1) :
  79.             days > 15 ? (type == "apartment" ? 0.5 : type == "president apartment" ? 0.8 : 1) :
  80.             (type == "apartment" ? 0.65 : type == "president apartment" ? 0.85 : 1)) *
  81.             (rating == "positive" ? 1.25 : 0.9) * days) : 0;
  82.  
  83.     console.log(`${price.toFixed(2)}`);
  84. }
  85.  
Add Comment
Please, Sign In to add comment