Advertisement
Spocoman

11. Fruit Shop

Dec 19th, 2021 (edited)
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fruitShop(input) {
  2.     let fruit = input[0];
  3.     let day = input[1];
  4.     let quantity = Number(input[2]);
  5.     let price = 0;
  6.    
  7.     if (day === "Monday" || day === "Tuesday" || day === "Wednesday" || day === "Thursday" || day === "Friday") {
  8.       if (fruit === "banana") {
  9.         price = 2.50;
  10.       } else if (fruit === "apple") {
  11.         price = 1.20;
  12.       } else if (fruit === "orange") {
  13.         price = 0.85;
  14.       } else if (fruit === "grapefruit") {
  15.         price = 1.45;
  16.       } else if (fruit === "kiwi") {
  17.         price = 2.70;
  18.       } else if (fruit === "pineapple") {
  19.         price = 5.50;
  20.       } else if (fruit === "grapes") {
  21.         price = 3.85;
  22.       }
  23.     } else if (day === "Saturday" || day === "Sunday") {
  24.       if (fruit === "banana") {
  25.         price = 2.70;
  26.       } else if (fruit === "apple") {
  27.         price = 1.25;
  28.       } else if (fruit === "orange") {
  29.         price = 0.90;
  30.       } else if (fruit === "grapefruit") {
  31.         price = 1.60;
  32.       } else if (fruit === "kiwi") {
  33.         price = 3.00;
  34.       } else if (fruit === "pineapple") {
  35.         price = 5.60;
  36.       } else if (fruit === "grapes") {
  37.         price = 4.20;
  38.       }
  39.     }
  40.     if (price === 0) {
  41.       console.log("error");
  42.     } else {
  43.       console.log((price * quantity).toFixed(2));
  44.     }
  45.   }
  46.  
  47. РЕШЕНИЕ СЪС SWITCH И ТЕРНАРЕН ОПЕРАТОР:
  48.  
  49. function fruitShop(input) {
  50.     let fruit = (input[0]);
  51.     let day = (input[1]);
  52.     let quantity = Number(input[2]);
  53.     let price = 0;
  54.  
  55.     switch (day) {
  56.         case "Monday":
  57.         case "Tuesday":
  58.         case "Wednesday":
  59.         case "Thursday":
  60.         case "Friday":
  61.             price = fruit === "banana" ? 2.50 :
  62.                     fruit === "apple" ? 1.20 :
  63.                     fruit === "orange" ? 0.85 :
  64.                     fruit === "grapefruit" ? 1.45 :
  65.                     fruit === "kiwi" ? 2.70 :
  66.                     fruit === "pineapple" ? 5.50 :
  67.                     fruit === "grapes" ? 3.85 : 0;
  68.             break;
  69.  
  70.         case "Saturday":
  71.         case "Sunday":
  72.             price = fruit === "banana" ? 2.70 :
  73.                     fruit === "apple" ? 1.25 :
  74.                     fruit === "orange" ? 0.90 :
  75.                     fruit === "grapefruit" ? 1.60 :
  76.                     fruit === "kiwi" ? 3.00 :
  77.                     fruit === "pineapple" ? 5.60 :
  78.                     fruit === "grapes" ? 4.20 : 0;
  79.             break;
  80.     }
  81.     console.log(price === 0 ? "error" : (price * quantity).toFixed(2));
  82. }
  83.  
  84. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
  85.  
  86. function fruitShop(input) {
  87.     let fruit = (input[0]);
  88.     let day = (input[1]);
  89.     let quantity = Number(input[2]);
  90.  
  91.     let price = (day === "Monday" ||
  92.                  day === "Tuesday" ||
  93.                  day === "Wednesday" ||
  94.                  day === "Thursday" ||
  95.                  day === "Friday") ? (fruit === "banana" ? 2.50 :
  96.                                       fruit === "apple" ? 1.20 :
  97.                                       fruit === "orange" ? 0.85 :
  98.                                       fruit === "grapefruit" ? 1.45 :
  99.                                       fruit === "kiwi" ? 2.70 :
  100.                                       fruit === "pineapple" ? 5.50 :
  101.                                       fruit === "grapes" ? 3.85 : 0) :
  102.                 (day === "Saturday" ||
  103.                  day === "Sunday") ? (fruit === "banana" ? 2.70 :
  104.                                       fruit === "apple" ? 1.25 :
  105.                                       fruit === "orange" ? 0.90 :
  106.                                       fruit === "grapefruit" ? 1.60 :
  107.                                       fruit === "kiwi" ? 3.00 :
  108.                                       fruit === "pineapple" ? 5.60 :
  109.                                        fruit === "grapes" ? 4.20 : 0) : 0;
  110.  
  111.    
  112.     console.log(price === 0 ? "error" : (price * quantity).toFixed(2));
  113. }
  114.  
  115. РЕШЕНИЯ С КОЛЕКЦИЯ И ТЕРНАРЕН ОПЕРАТОР:
  116.  
  117. function fruitShop(input) {
  118.     let fruit = input[0];
  119.     let day = input[1];
  120.     let quantity = Number(input[2]);
  121.     let price = 0;
  122.  
  123.     if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].includes(day)) {
  124.         price = fruit === "banana" ? 2.50 :
  125.                 fruit === "apple" ? 1.20 :
  126.                 fruit === "orange" ? 0.85 :
  127.                 fruit === "grapefruit" ? 1.45 :
  128.                 fruit === "kiwi" ? 2.70 :
  129.                 fruit === "pineapple" ? 5.50 :
  130.                 fruit === "grapes" ? 3.85 : 0;
  131.  
  132.     } else if (["Saturday", "Sunday"].includes(day)) {
  133.         price = fruit === "banana" ? 2.70 :
  134.                 fruit === "apple" ? 1.25 :
  135.                 fruit === "orange" ? 0.90 :
  136.                 fruit === "grapefruit" ? 1.60 :
  137.                 fruit === "kiwi" ? 3.00 :
  138.                 fruit === "pineapple" ? 5.60 :
  139.                 fruit === "grapes" ? 4.20 : 0;
  140.     }
  141.     console.log(price === 0 ? "error" : (price * quantity).toFixed(2));
  142. }
  143.  
  144. ИЛИ:
  145.  
  146. function fruitShop(input) {
  147.     let fruit = input[0];
  148.     let day = input[1];
  149.     let quantity = Number(input[2]);
  150.     let price = 0;
  151.  
  152.     if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].includes(day)) {
  153.         let weekdayPrice = { banana: 2.50, apple: 1.20, orange: 0.85, grapefruit: 1.45, kiwi: 2.70, pineapple: 5.5, grapes: 3.85 };
  154.         if (weekdayPrice.hasOwnProperty(fruit)) {
  155.             price = weekdayPrice[fruit];
  156.         }
  157.     } else if (["Saturday", "Sunday"].includes(day)) {
  158.         let weekendPrice = { banana: 2.70, apple: 1.25, orange: 0.90, grapefruit: 1.60, kiwi: 3.00, pineapple: 5.60, grapes: 4.20 };
  159.         if (weekendPrice.hasOwnProperty(fruit)) {
  160.             price = weekendPrice[fruit];
  161.         }
  162.     }
  163.     console.log(price === 0 ? "error" : (price * quantity).toFixed(2));
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement