Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function newHouse(input) {
- let flower = input[0];
- let quantity = Number(input[1]);
- let budget = Number(input[2]);
- let sum = 0;
- if (flower === "Roses") {
- sum = quantity * 5.00;
- if (quantity > 80) {
- sum *= 0.90;
- }
- } else if (flower === "Dahlias") {
- sum = quantity * 3.80;
- if (quantity > 90) {
- sum *= 0.85;
- }
- } else if (flower === "Tulips") {
- sum = quantity * 2.80;
- if (quantity > 80) {
- sum *= 0.85;
- }
- } else if (flower === "Narcissus") {
- sum = quantity * 3.00;
- if (quantity < 120) {
- sum *= 1.15;
- }
- } else if (flower === "Gladiolus") {
- sum = quantity * 2.50;
- if (quantity < 80) {
- sum *= 1.20;
- }
- }
- if (sum > budget) {
- console.log(`Not enough money, you need ${(sum - budget).toFixed(2)} leva more.`);
- } else {
- console.log(`Hey, you have a great garden with ${quantity} ${flower} and ${(budget - sum).toFixed(2)} leva left.`);
- }
- }
- РЕШЕНИЕ СЪС SWITCH:
- function newHouse(input) {
- let flower = input[0];
- let quantity = Number(input[1]);
- let budget = Number(input[2]);
- let sum = 0;
- switch (flower) {
- case "Roses":
- sum = quantity * 5.00;
- if (quantity > 80) {
- sum *= 0.90;
- }
- break;
- case "Dahlias":
- sum = quantity * 3.80;
- if (quantity > 90) {
- sum *= 0.85;
- }
- break;
- case "Tulips":
- sum = quantity * 2.80;
- if (quantity > 80) {
- sum *= 0.85;
- }
- break;
- case "Narcissus":
- sum = quantity * 3.00;
- if (quantity < 120) {
- sum *= 1.15;
- }
- break;
- case "Gladiolus":
- sum = quantity * 2.50;
- if (quantity < 80) {
- sum *= 1.20;
- }
- break;
- }
- if (sum > budget) {
- console.log(`Not enough money, you need ${(sum - budget).toFixed(2)} leva more.`);
- } else {
- console.log(`Hey, you have a great garden with ${quantity} ${flower} and ${(budget - sum).toFixed(2)} leva left.`);
- }
- }
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
- function newHouse(input) {
- let flower = input[0];
- let quantity = Number(input[1]);
- let budget = Number(input[2]);
- let sum = (flower === "Roses" ? 5.00 * (quantity > 80 ? 0.90 : 1) :
- flower === "Dahlias" ? 3.80 * (quantity > 90 ? 0.85 : 1) :
- flower === "Tulips" ? 2.80 * (quantity > 80 ? 0.85 : 1) :
- flower === "Narcissus" ? 3.00 * (quantity < 120 ? 1.15 : 1) :
- flower === "Gladiolus" ? 2.50 * (quantity < 80 ? 1.20 : 1) : 0) * quantity;
- console.log(sum > budget ? `Not enough money, you need ${(sum - budget).toFixed(2)} leva more.` :
- `Hey, you have a great garden with ${quantity} ${flower} and ${(budget - sum).toFixed(2)} leva left.`);
- }
- ИЛИ С ИЗВАЖДАНЕ ОТ БЮДЖЕТА:
- function newHouse(input) {
- let flower = input[0];
- let quantity = Number(input[1]);
- let budget = Number(input[2]);
- budget -= (flower === "Roses" ? 5.00 * (quantity > 80 ? 0.90 : 1) :
- flower === "Dahlias" ? 3.80 * (quantity > 90 ? 0.85 : 1) :
- flower === "Tulips" ? 2.80 * (quantity > 80 ? 0.85 : 1) :
- flower === "Narcissus" ? 3.00 * (quantity < 120 ? 1.15 : 1) :
- flower === "Gladiolus" ? 2.50 * (quantity < 80 ? 1.20 : 1) : 0) * quantity;
- console.log(budget < 0 ? `Not enough money, you need ${Math.abs(budget).toFixed(2)} leva more.` :
- `Hey, you have a great garden with ${quantity} ${flower} and ${budget.toFixed(2)} leva left.`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement