Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function journey(input) {
- let budget = Number(input[0]);
- let season = input[1];
- let place = "";
- let region = "";
- let sum = 0;
- switch (season) {
- case "summer":
- place = "Camp";
- if (budget <= 100) {
- sum = 0.3 * budget;
- } else if (budget > 100 && budget <= 1000) {
- sum = 0.4 * budget;
- }
- break;
- case "winter":
- place = "Hotel";
- if (budget <= 100) {
- sum = 0.7 * budget;
- } else if (budget > 100 && budget <= 1000) {
- sum = 0.8 * budget;
- }
- break;
- }
- if (budget <= 100) {
- region = "Bulgaria";
- } else if (budget > 100 && budget <= 1000) {
- region = "Balkans";
- } else {
- place = "Hotel";
- region = "Europe";
- sum = 0.9 * budget;
- }
- console.log(`Somewhere in ${region}`);
- console.log(`${place} - ${sum.toFixed(2)}`);
- }
- Решение с if-else леко тарикатската:
- function journey(input) {
- let budget = Number(input[0]);
- let season = input[1];
- let place = "Hotel";
- let region = "Europe";
- if (budget <= 100) {
- region = "Bulgaria";
- if (season === "summer") {
- place = "Camp";
- budget *= 0.3;
- } else {
- budget *= 0.7;
- }
- } else if (budget > 100 && budget <= 1000) {
- region = "Balkans";
- if (season === "summer") {
- place = "Camp";
- budget *= 0.4;
- } else {
- budget *= 0.8;
- }
- } else {
- budget *= 0.9;
- }
- console.log(`Somewhere in ${region}`);
- console.log(`${place} - ${budget.toFixed(2)}`);
- }
- Pешение с тернарен оператор:
- function journey(input) {
- let budget = Number(input[0]);
- let season = input[1];
- let place = season == "summer" && budget <= 1000 ? "Camp" : "Hotel";
- let region = budget <= 100 ? "Bulgaria" : budget > 1000 ? "Europe" : "Balkans";
- budget *= budget <= 100 ? (season == "summer" ? 0.3 : 0.7) :
- budget > 100 && budget <= 1000 ? (season == "summer" ? 0.4 : 0.8) : 0.9;
- console.log(`Somewhere in ${region}`);
- console.log(`${place} - ${budget.toFixed(2)}`);
- }
Add Comment
Please, Sign In to add comment