Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- РЕШЕНИЕ С IF ELSE:
- function tradeCommissions(input) {
- let town = input[0];
- let profit = Number(input[1]);
- let commission = 0;
- if (town === "Sofia") {
- if (profit > 0 && profit <= 500) {
- commission = 0.05;
- } else if (profit > 500 && profit <= 1000) {
- commission = 0.07;
- } else if (profit > 1000 && profit <= 10000) {
- commission = 0.08;
- } else if (profit > 10000) {
- commission = 0.12;
- }
- } else if (town === "Varna") {
- if (profit > 0 && profit <= 500) {
- commission = 0.045;
- } else if (profit > 500 && profit <= 1000) {
- commission = 0.075;
- } else if (profit > 1000 && profit <= 10000) {
- commission = 0.1;
- } else if (profit > 10000) {
- commission = 0.13;
- }
- } else if (town === "Plovdiv") {
- if (profit > 0 && profit <= 500) {
- commission = 0.055;
- } else if (profit > 500 && profit <= 1000) {
- commission = 0.08;
- } else if (profit > 1000 && profit <= 10000) {
- commission = 0.12;
- } else if (profit > 10000) {
- commission = 0.145;
- }
- }
- let sum = profit * commission;
- if (sum > 0) {
- console.log(sum.toFixed(2));
- } else {
- console.log("error");
- }
- }
- РЕШЕНИЕ СЪС SWITCH I IF ELSE:
- function tradeCommissions(input) {
- let town = input[0];
- let profit = Number(input[1]);
- let commission = 0;
- switch (town) {
- case "Sofia":
- if (profit > 0 && profit <= 500) {
- commission = 0.05;
- } else if (profit > 500 && profit <= 1000) {
- commission = 0.07;
- } else if (profit > 1000 && profit <= 10000) {
- commission = 0.08;
- } else if (profit > 10000) {
- commission = 0.12;
- }
- break;
- case "Varna":
- if (profit > 0 && profit <= 500) {
- commission = 0.045;
- } else if (profit > 500 && profit <= 1000) {
- commission = 0.075;
- } else if (profit > 1000 && profit <= 10000) {
- commission = 0.1;
- } else if (profit > 10000) {
- commission = 0.13;
- }
- break;
- case "Plovdiv":
- if (profit > 0 && profit <= 500) {
- commission = 0.055;
- } else if (profit > 500 && profit <= 1000) {
- commission = 0.08;
- } else if (profit > 1000 && profit <= 10000) {
- commission = 0.12;
- } else if (profit > 10000) {
- commission = 0.145;
- }
- break;
- }
- let sum = profit * commission;
- if (sum > 0) {
- console.log(sum.toFixed(2));
- } else {
- console.log("error");
- }
- }
- РЕШЕНИЕ С IF ELSE И ТЕРНАРЕН ОПЕРАТОР:
- function tradeCommissions(input) {
- let town = input[0];
- let sum = Number(input[1]);
- if (sum > 0 && sum <= 500) {
- sum *= town === "Sofia" ? 0.05 : town === "Varna" ? 0.045 : town === "Plovdiv" ? 0.055 : 0;
- } else if (sum > 500 && sum <= 1000) {
- sum *= town === "Sofia" ? 0.07 : town === "Varna" ? 0.075 : town === "Plovdiv" ? 0.08 : 0;
- } else if (sum > 1000 && sum <= 10000) {
- sum *= town === "Sofia" ? 0.08 : town === "Varna" ? 0.1 : town === "Plovdiv" ? 0.12 : 0;
- } else if (sum > 10000) {
- sum *= town === "Sofia" ? 0.12 : town === "Varna" ? 0.13 : town === "Plovdiv" ? 0.145 : 0;
- }
- console.log(sum > 0 ? sum.toFixed(2) : "error");
- }
- РЕШЕНИЕ С КОЛЕКЦИЯ И ТЕРНАРЕН ОПЕРАТОР:
- function tradeCommissions(input) {
- let town = input[0];
- let profit = Number(input[1]);
- let commission = {
- "Sofia": profit > 0 && profit <= 500 ? 0.05 : profit > 500 && profit <= 1000 ? 0.07 : profit > 1000 && profit <= 10000 ? 0.08 : profit > 10000 ? 0.12 : 0,
- "Varna": profit > 0 && profit <= 500 ? 0.045 : profit > 500 && profit <= 1000 ? 0.075 : profit > 1000 && profit <= 10000 ? 0.1 : profit > 10000 ? 0.13 : 0,
- "Plovdiv": profit > 0 && profit <= 500 ? 0.055 : profit > 500 && profit <= 1000 ? 0.08 : profit > 1000 && profit <= 10000 ? 0.12 : profit > 10000 ? 0.145 : 0,
- };
- let sum = profit * (commission.hasOwnProperty(town) ? commission[town] : 0);
- console.log(sum > 0 ? sum.toFixed(2) : "error");
- }
- ИЛИ САМО С ТЕРНАРЕН ОПЕРАТОР:
- function tradeCommissions(input) {
- let town = input[0];
- let sum = Number(input[1]);
- sum *= sum > 0 && sum <= 500 ? (town === "Sofia" ? 0.05 : town === "Varna" ? 0.045 : town === "Plovdiv" ? 0.055 : 0) :
- sum > 500 && sum <= 1000 ? (town === "Sofia" ? 0.07 : town === "Varna" ? 0.075 : town === "Plovdiv" ? 0.08 : 0) :
- sum > 1000 && sum <= 10000 ? (town === "Sofia" ? 0.08 : town === "Varna" ? 0.1 : town === "Plovdiv" ? 0.12 : 0) :
- sum > 10000 ? (town === "Sofia" ? 0.12 : town === "Varna" ? 0.13 : town === "Plovdiv" ? 0.145 : 0) : 0;
- console.log(sum > 0 ? sum.toFixed(2) : "error");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement