Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function fruitShop(input) {
- let fruit = input[0];
- let day = input[1];
- let quantity = Number(input[2]);
- let price = 0;
- if (day === "Monday" || day === "Tuesday" || day === "Wednesday" || day === "Thursday" || day === "Friday") {
- if (fruit === "banana") {
- price = 2.50;
- } else if (fruit === "apple") {
- price = 1.20;
- } else if (fruit === "orange") {
- price = 0.85;
- } else if (fruit === "grapefruit") {
- price = 1.45;
- } else if (fruit === "kiwi") {
- price = 2.70;
- } else if (fruit === "pineapple") {
- price = 5.50;
- } else if (fruit === "grapes") {
- price = 3.85;
- }
- } else if (day === "Saturday" || day === "Sunday") {
- if (fruit === "banana") {
- price = 2.70;
- } else if (fruit === "apple") {
- price = 1.25;
- } else if (fruit === "orange") {
- price = 0.90;
- } else if (fruit === "grapefruit") {
- price = 1.60;
- } else if (fruit === "kiwi") {
- price = 3.00;
- } else if (fruit === "pineapple") {
- price = 5.60;
- } else if (fruit === "grapes") {
- price = 4.20;
- }
- }
- if (price === 0) {
- console.log("error");
- } else {
- console.log((price * quantity).toFixed(2));
- }
- }
- РЕШЕНИЕ СЪС SWITCH И ТЕРНАРЕН ОПЕРАТОР:
- function fruitShop(input) {
- let fruit = (input[0]);
- let day = (input[1]);
- let quantity = Number(input[2]);
- let price = 0;
- switch (day) {
- case "Monday":
- case "Tuesday":
- case "Wednesday":
- case "Thursday":
- case "Friday":
- price = fruit === "banana" ? 2.50 :
- fruit === "apple" ? 1.20 :
- fruit === "orange" ? 0.85 :
- fruit === "grapefruit" ? 1.45 :
- fruit === "kiwi" ? 2.70 :
- fruit === "pineapple" ? 5.50 :
- fruit === "grapes" ? 3.85 : 0;
- break;
- case "Saturday":
- case "Sunday":
- price = fruit === "banana" ? 2.70 :
- fruit === "apple" ? 1.25 :
- fruit === "orange" ? 0.90 :
- fruit === "grapefruit" ? 1.60 :
- fruit === "kiwi" ? 3.00 :
- fruit === "pineapple" ? 5.60 :
- fruit === "grapes" ? 4.20 : 0;
- break;
- }
- console.log(price === 0 ? "error" : (price * quantity).toFixed(2));
- }
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
- function fruitShop(input) {
- let fruit = (input[0]);
- let day = (input[1]);
- let quantity = Number(input[2]);
- let price = (day === "Monday" ||
- day === "Tuesday" ||
- day === "Wednesday" ||
- day === "Thursday" ||
- day === "Friday") ? (fruit === "banana" ? 2.50 :
- fruit === "apple" ? 1.20 :
- fruit === "orange" ? 0.85 :
- fruit === "grapefruit" ? 1.45 :
- fruit === "kiwi" ? 2.70 :
- fruit === "pineapple" ? 5.50 :
- fruit === "grapes" ? 3.85 : 0) :
- (day === "Saturday" ||
- day === "Sunday") ? (fruit === "banana" ? 2.70 :
- fruit === "apple" ? 1.25 :
- fruit === "orange" ? 0.90 :
- fruit === "grapefruit" ? 1.60 :
- fruit === "kiwi" ? 3.00 :
- fruit === "pineapple" ? 5.60 :
- fruit === "grapes" ? 4.20 : 0) : 0;
- console.log(price === 0 ? "error" : (price * quantity).toFixed(2));
- }
- РЕШЕНИЯ С КОЛЕКЦИЯ И ТЕРНАРЕН ОПЕРАТОР:
- function fruitShop(input) {
- let fruit = input[0];
- let day = input[1];
- let quantity = Number(input[2]);
- let price = 0;
- if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].includes(day)) {
- price = fruit === "banana" ? 2.50 :
- fruit === "apple" ? 1.20 :
- fruit === "orange" ? 0.85 :
- fruit === "grapefruit" ? 1.45 :
- fruit === "kiwi" ? 2.70 :
- fruit === "pineapple" ? 5.50 :
- fruit === "grapes" ? 3.85 : 0;
- } else if (["Saturday", "Sunday"].includes(day)) {
- price = fruit === "banana" ? 2.70 :
- fruit === "apple" ? 1.25 :
- fruit === "orange" ? 0.90 :
- fruit === "grapefruit" ? 1.60 :
- fruit === "kiwi" ? 3.00 :
- fruit === "pineapple" ? 5.60 :
- fruit === "grapes" ? 4.20 : 0;
- }
- console.log(price === 0 ? "error" : (price * quantity).toFixed(2));
- }
- ИЛИ:
- function fruitShop(input) {
- let fruit = input[0];
- let day = input[1];
- let quantity = Number(input[2]);
- let price = 0;
- if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].includes(day)) {
- let weekdayPrice = { banana: 2.50, apple: 1.20, orange: 0.85, grapefruit: 1.45, kiwi: 2.70, pineapple: 5.5, grapes: 3.85 };
- if (weekdayPrice.hasOwnProperty(fruit)) {
- price = weekdayPrice[fruit];
- }
- } else if (["Saturday", "Sunday"].includes(day)) {
- let weekendPrice = { banana: 2.70, apple: 1.25, orange: 0.90, grapefruit: 1.60, kiwi: 3.00, pineapple: 5.60, grapes: 4.20 };
- if (weekendPrice.hasOwnProperty(fruit)) {
- price = weekendPrice[fruit];
- }
- }
- console.log(price === 0 ? "error" : (price * quantity).toFixed(2));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement