Advertisement
Spocoman

12. Trade Commissions

Dec 19th, 2021 (edited)
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. РЕШЕНИЕ С IF ELSE:
  2.  
  3. function tradeCommissions(input) {
  4.     let town = input[0];
  5.     let profit = Number(input[1]);
  6.     let commission = 0;
  7.  
  8.     if (town === "Sofia") {
  9.         if (profit > 0 && profit <= 500) {
  10.             commission = 0.05;
  11.         } else if (profit > 500 && profit <= 1000) {
  12.             commission = 0.07;
  13.         } else if (profit > 1000 && profit <= 10000) {
  14.             commission = 0.08;
  15.         } else if (profit > 10000) {
  16.             commission = 0.12;
  17.         }
  18.     } else if (town === "Varna") {
  19.         if (profit > 0 && profit <= 500) {
  20.             commission = 0.045;
  21.         } else if (profit > 500 && profit <= 1000) {
  22.             commission = 0.075;
  23.         } else if (profit > 1000 && profit <= 10000) {
  24.             commission = 0.1;
  25.         } else if (profit > 10000) {
  26.             commission = 0.13;
  27.         }
  28.     } else if (town === "Plovdiv") {
  29.         if (profit > 0 && profit <= 500) {
  30.             commission = 0.055;
  31.         } else if (profit > 500 && profit <= 1000) {
  32.             commission = 0.08;
  33.         } else if (profit > 1000 && profit <= 10000) {
  34.             commission = 0.12;
  35.         } else if (profit > 10000) {
  36.             commission = 0.145;
  37.         }
  38.     }
  39.  
  40.     let sum = profit * commission;
  41.  
  42.     if (sum > 0) {
  43.         console.log(sum.toFixed(2));
  44.     } else {
  45.         console.log("error");
  46.     }
  47. }
  48.  
  49. РЕШЕНИЕ СЪС SWITCH I IF ELSE:
  50.  
  51. function tradeCommissions(input) {
  52.     let town = input[0];
  53.     let profit = Number(input[1]);
  54.     let commission = 0;
  55.  
  56.     switch (town) {
  57.         case "Sofia":
  58.             if (profit > 0 && profit <= 500) {
  59.                 commission = 0.05;
  60.             } else if (profit > 500 && profit <= 1000) {
  61.                 commission = 0.07;
  62.             } else if (profit > 1000 && profit <= 10000) {
  63.                 commission = 0.08;
  64.             } else if (profit > 10000) {
  65.                 commission = 0.12;
  66.             }
  67.             break;
  68.  
  69.         case "Varna":
  70.             if (profit > 0 && profit <= 500) {
  71.                 commission = 0.045;
  72.             } else if (profit > 500 && profit <= 1000) {
  73.                 commission = 0.075;
  74.             } else if (profit > 1000 && profit <= 10000) {
  75.                 commission = 0.1;
  76.             } else if (profit > 10000) {
  77.                 commission = 0.13;
  78.             }
  79.             break;
  80.  
  81.         case "Plovdiv":
  82.             if (profit > 0 && profit <= 500) {
  83.                 commission = 0.055;
  84.             } else if (profit > 500 && profit <= 1000) {
  85.                 commission = 0.08;
  86.             } else if (profit > 1000 && profit <= 10000) {
  87.                 commission = 0.12;
  88.             } else if (profit > 10000) {
  89.                 commission = 0.145;
  90.             }
  91.             break;
  92.     }
  93.  
  94.     let sum = profit * commission;
  95.  
  96.     if (sum > 0) {
  97.         console.log(sum.toFixed(2));
  98.     } else {
  99.         console.log("error");
  100.     }
  101. }
  102.  
  103. РЕШЕНИЕ С IF ELSE И ТЕРНАРЕН ОПЕРАТОР:
  104.  
  105. function tradeCommissions(input) {
  106.     let town = input[0];
  107.     let sum = Number(input[1]);
  108.  
  109.     if (sum > 0 && sum <= 500) {
  110.         sum *= town === "Sofia" ? 0.05 : town === "Varna" ? 0.045 : town === "Plovdiv" ? 0.055 : 0;
  111.     } else if (sum > 500 && sum <= 1000) {
  112.         sum *= town === "Sofia" ? 0.07 : town === "Varna" ? 0.075 : town === "Plovdiv" ? 0.08 : 0;
  113.     } else if (sum > 1000 && sum <= 10000) {
  114.         sum *= town === "Sofia" ? 0.08 : town === "Varna" ? 0.1 : town === "Plovdiv" ? 0.12 : 0;
  115.     } else if (sum > 10000) {
  116.         sum *= town === "Sofia" ? 0.12 : town === "Varna" ? 0.13 : town === "Plovdiv" ? 0.145 : 0;
  117.     }
  118.     console.log(sum > 0 ? sum.toFixed(2) : "error");
  119. }
  120.  
  121. РЕШЕНИЕ С КОЛЕКЦИЯ И ТЕРНАРЕН ОПЕРАТОР:
  122.  
  123. function tradeCommissions(input) {
  124.     let town = input[0];
  125.     let profit = Number(input[1]);
  126.  
  127.     let commission = {
  128.         "Sofia": profit > 0 && profit <= 500 ? 0.05 : profit > 500 && profit <= 1000 ? 0.07 : profit > 1000 && profit <= 10000 ? 0.08 : profit > 10000 ? 0.12 : 0,
  129.         "Varna": profit > 0 && profit <= 500 ? 0.045 : profit > 500 && profit <= 1000 ? 0.075 : profit > 1000 && profit <= 10000 ? 0.1 : profit > 10000 ? 0.13 : 0,
  130.         "Plovdiv": profit > 0 && profit <= 500 ? 0.055 : profit > 500 && profit <= 1000 ? 0.08 : profit > 1000 && profit <= 10000 ? 0.12 : profit > 10000 ? 0.145 : 0,
  131.     };
  132.  
  133.     let sum = profit * (commission.hasOwnProperty(town) ? commission[town] : 0);
  134.  
  135.     console.log(sum > 0 ? sum.toFixed(2) : "error");
  136. }
  137.  
  138. ИЛИ САМО С ТЕРНАРЕН ОПЕРАТОР:
  139.  
  140. function tradeCommissions(input) {
  141.     let town = input[0];
  142.     let sum = Number(input[1]);
  143.  
  144.     sum *= sum > 0 && sum <= 500 ? (town === "Sofia" ? 0.05 : town === "Varna" ? 0.045 : town === "Plovdiv" ? 0.055 : 0) :
  145.            sum > 500 && sum <= 1000 ? (town === "Sofia" ? 0.07 : town === "Varna" ? 0.075 : town === "Plovdiv" ? 0.08 : 0) :
  146.            sum > 1000 && sum <= 10000 ? (town === "Sofia" ? 0.08 : town === "Varna" ? 0.1 : town === "Plovdiv" ? 0.12 : 0) :
  147.            sum > 10000 ? (town === "Sofia" ? 0.12 : town === "Varna" ? 0.13 : town === "Plovdiv" ? 0.145 : 0) : 0;
  148.  
  149.     console.log(sum > 0 ? sum.toFixed(2) : "error");
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement