Spocoman

05. Journey

Dec 20th, 2021 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function journey(input) {
  2.     let budget = Number(input[0]);
  3.     let season = input[1];
  4.     let place = "";
  5.     let region = "";
  6.     let sum = 0;
  7.  
  8.     switch (season) {
  9.         case "summer":
  10.             place = "Camp";
  11.             if (budget <= 100) {
  12.                 sum = 0.3 * budget;
  13.             } else if (budget > 100 && budget <= 1000) {
  14.                 sum = 0.4 * budget;
  15.             }
  16.             break;
  17.  
  18.         case "winter":
  19.             place = "Hotel";
  20.             if (budget <= 100) {
  21.                 sum = 0.7 * budget;
  22.             } else if (budget > 100 && budget <= 1000) {
  23.                 sum = 0.8 * budget;
  24.             }
  25.             break;
  26.     }
  27.     if (budget <= 100) {
  28.         region = "Bulgaria";
  29.     } else if (budget > 100 && budget <= 1000) {
  30.         region = "Balkans";
  31.     } else {
  32.         place = "Hotel";
  33.         region = "Europe";
  34.         sum = 0.9 * budget;
  35.     }
  36.     console.log(`Somewhere in ${region}`);
  37.     console.log(`${place} - ${sum.toFixed(2)}`);
  38. }
  39.  
  40. Решение с if-else леко тарикатската:
  41.  
  42. function journey(input) {
  43.     let budget = Number(input[0]);
  44.     let season = input[1];
  45.     let place = "Hotel";
  46.     let region = "Europe";
  47.  
  48.     if (budget <= 100) {
  49.         region = "Bulgaria";
  50.         if (season === "summer") {
  51.             place = "Camp";
  52.             budget *= 0.3;
  53.         } else {
  54.             budget *= 0.7;
  55.         }
  56.     } else if (budget > 100 && budget <= 1000) {
  57.         region = "Balkans";
  58.         if (season === "summer") {
  59.             place = "Camp";
  60.             budget *= 0.4;
  61.         } else {
  62.             budget *= 0.8;
  63.         }
  64.     } else {
  65.         budget *= 0.9;
  66.     }
  67.     console.log(`Somewhere in ${region}`);
  68.     console.log(`${place} - ${budget.toFixed(2)}`);
  69. }
  70.  
  71. Pешение с тернарен оператор:
  72.  
  73. function journey(input) {
  74.     let budget = Number(input[0]);
  75.     let season = input[1];
  76.     let place = season == "summer" && budget <= 1000 ? "Camp" : "Hotel";
  77.     let region = budget <= 100 ? "Bulgaria" : budget > 1000 ? "Europe" : "Balkans";
  78.  
  79.     budget *= budget <= 100 ? (season == "summer" ? 0.3 : 0.7) :
  80.         budget > 100 && budget <= 1000 ? (season == "summer" ? 0.4 : 0.8) : 0.9;
  81.  
  82.     console.log(`Somewhere in ${region}`);
  83.     console.log(`${place} - ${budget.toFixed(2)}`);
  84. }
  85.  
Add Comment
Please, Sign In to add comment