Spocoman

Coffee Machine

Jan 8th, 2022 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function coffeeMachine(input) {
  2.     let coffee = input[0];
  3.     let sugar = input[1];
  4.     let count = Number(input[2]);
  5.     let sum = count;
  6.  
  7.     if (coffee === "Espresso") {
  8.         if (sugar === "Without") {
  9.             sum *= 0.9 * 0.65;
  10.         } else if (sugar === "Extra") {
  11.             sum *= 1.2;
  12.         }
  13.         if (count > 5) {
  14.             sum *= 0.75;
  15.         }
  16.     } else if (coffee === "Cappuccino") {
  17.         if (sugar === "Without") {
  18.             sum *= 0.65;
  19.         } else if (sugar === "Normal") {
  20.             sum *= 1.2;
  21.         } else {
  22.             sum *= 1.6;
  23.         }
  24.     } else {
  25.         if (sugar === "Without") {
  26.             sum *= 0.5 * 0.65;
  27.         } else if (sugar === "Normal") {
  28.             sum *= 0.6;
  29.         } else {
  30.             sum *= 0.7;
  31.         }
  32.     }
  33.     if (sum > 15) {
  34.         sum *= 0.8;
  35.     }
  36.     console.log(`You bought ${count} cups of ${coffee} for ${sum.toFixed(2)} lv.`);
  37. }
  38.  
  39. РЕШЕНИЕ СЪС SWITCH И ТЕРНАРЕН ОПЕРАТОР:
  40.  
  41. function coffeeMachine(input) {
  42.     let coffee = input[0];
  43.     let sugar = input[1];
  44.     let count = Number(input[2]);
  45.     let sum = count;
  46.  
  47.     switch (coffee) {
  48.         case "Espresso":
  49.             sum *= (sugar === "Without" ? 0.9 * 0.65 : sugar === "Extra" ? 1.2 : 1) * (count > 5 ? 0.75 : 1);
  50.             break;
  51.  
  52.         case "Cappuccino":
  53.             sum *= sugar === "Without" ? 0.65 : sugar === "Extra" ? 1.6 : 1.2;
  54.             break;
  55.  
  56.         case "Tea":
  57.             sum *= sugar === "Without" ? 0.5 * 0.65 : sugar === "Extra" ? 0.7 : 0.6;
  58.             break;
  59.     }
  60.     console.log(`You bought ${count} cups of ${coffee} for ${(sum * (sum > 15 ? 0.8 : 1)).toFixed(2)} lv.`);
  61. }
  62.  
  63. РЕШЕНИЕ САМО С ТЕРНАРЕН ОПЕРАТОР:
  64.  
  65. function coffeeMachine(input) {
  66.     let coffee = input[0];
  67.     let sugar = input[1];
  68.     let count = Number(input[2]);
  69.  
  70.     let sum = (coffee === "Espresso" ? ((sugar === "Without" ? 0.9 * 0.65 : sugar === "Extra" ? 1.2 : 1) * (count > 5 ? 0.75 : 1)) :
  71.                coffee === "Cappuccino" ? (sugar === "Without" ? 0.65 : sugar === "Extra" ? 1.6 : 1.2) :
  72.                (sugar === "Without" ? 0.5 * 0.65 : sugar === "Extra" ? 0.7 : 0.6)) * count;
  73.  
  74.     console.log(`You bought ${count} cups of ${coffee} for ${(sum * (sum > 15 ? 0.8 : 1)).toFixed(2)} lv.`);
  75. }
  76.  
Add Comment
Please, Sign In to add comment