Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Решение с if-else:
- function smallShop(input) {
- let product = input[0];
- let town = input[1];
- let quantity = parseFloat(input[2]);
- let sum = 0;
- if (town === "Sofia") {
- if (product === "coffee") {
- sum = 0.5;
- } else if (product === "water") {
- sum = 0.8;
- } else if (product === "beer") {
- sum = 1.2;
- } else if (product === "sweets") {
- sum = 1.45;
- } else if (product === "peanuts") {
- sum = 1.6;
- }
- } else if (town === "Plovdiv") {
- if (product === "coffee") {
- sum = 0.4;
- } else if (product === "water") {
- sum = 0.7;
- } else if (product === "beer") {
- sum = 1.15;
- } else if (product === "sweets") {
- sum = 1.30;
- } else if (product === "peanuts") {
- sum = 1.5;
- }
- } else if (town === "Varna") {
- if (product === "coffee") {
- sum = 0.45;
- } else if (product === "water") {
- sum = 0.7;
- } else if (product === "beer") {
- sum = 1.1;
- } else if (product === "sweets") {
- sum = 1.35;
- } else if (product === "peanuts") {
- sum = 1.55;
- }
- }
- console.log(sum * quantity);
- }
- Решение със if-else и switch:
- function smallShop(input) {
- let product = input[0];
- let town = input[1];
- let quantity = parseFloat(input[2]);
- let sum = 0;
- if (town === "Sofia") {
- switch (product) {
- case "coffee":
- sum = 0.5;
- break;
- case "water":
- sum = 0.8;
- break;
- case "beer":
- sum = 1.2;
- break;
- case "sweets":
- sum = 1.45;
- break;
- case "peanuts":
- sum = 1.6;
- break;
- }
- } else if (town === "Plovdiv") {
- switch (product) {
- case "coffee":
- sum = 0.4;
- break;
- case "water":
- sum = 0.7;
- break;
- case "beer":
- sum = 1.15;
- break;
- case "sweets":
- sum = 1.30;
- break;
- case "peanuts":
- sum = 1.5;
- break;
- }
- } else if (town === "Varna") {
- switch (product) {
- case "coffee":
- sum = 0.45;
- break;
- case "water":
- sum = 0.7;
- break;
- case "beer":
- sum = 1.1;
- break;
- case "sweets":
- sum = 1.35;
- break;
- case "peanuts":
- sum = 1.55;
- break;
- }
- }
- console.log(sum * quantity);
- }
- Тарикатско за напреднали:)
- function smallShop(input) {
- let product = input[0];
- let town = input[1];
- let quantity = parseFloat(input[2]);
- let products = {};
- if (town === "Sofia") {
- products = { 'coffee': 0.5, 'water': 0.8, 'beer': 1.2, 'sweets': 1.45, 'peanuts': 1.6 };
- } else if (town === "Plovdiv") {
- products = { 'coffee': 0.4, 'water': 0.7, 'beer': 1.15, 'sweets': 1.3, 'peanuts': 1.5 };
- } else if (town === "Varna") {
- products = { 'coffee': 0.45, 'water': 0.7, 'beer': 1.1, 'sweets': 1.35, 'peanuts': 1.55 };
- }
- console.log(products[product] * quantity);
- }
- Или с тернарен оператор:
- function smallShop(input) {
- let product = input[0];
- let town = input[1];
- let quantity = parseFloat(input[2]);
- let products = town === "Sofia" ? { 'coffee': 0.5, 'water': 0.8, 'beer': 1.2, 'sweets': 1.45, 'peanuts': 1.6 } :
- town === "Plovdiv" ? { 'coffee': 0.4, 'water': 0.7, 'beer': 1.15, 'sweets': 1.3, 'peanuts': 1.5 } :
- town === "Varna" ? { 'coffee': 0.45, 'water': 0.7, 'beer': 1.1, 'sweets': 1.35, 'peanuts': 1.55 } : 0;
- console.log(products[product] * quantity);
- }
Add Comment
Please, Sign In to add comment