-Enigmos-

excursionSales.js

Oct 30th, 2021 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function excursionSales(input) {
  2.     let index = 0;
  3.     let seaTrip = Number(input[index]);
  4.     index++;
  5.     let mountainTrip = Number(input[index]);
  6.     index++;
  7.     let command = input[index];
  8.     index++;
  9.     let profit = 0;
  10.     let allTripsSold = false;
  11.  
  12.     while (command !== "Stop") {
  13.         if (command === "sea") {
  14.             if (seaTrip > 0) {
  15.                 seaTrip--;
  16.                 profit += 680;
  17.             }
  18.         } else if (command === "mountain") {
  19.             if (mountainTrip > 0) {
  20.                 mountainTrip--;
  21.                 profit += 499;
  22.             }
  23.         }
  24.  
  25.         if (seaTrip === 0 && mountainTrip === 0) {
  26.             allTripsSold = true;
  27.             console.log("Good job! Everything is sold.");
  28.             console.log(`Profit: ${profit} leva.`);
  29.             break;
  30.         }
  31.  
  32.         command = input[index];
  33.         index++;
  34.     }
  35.    
  36.     if (!allTripsSold) {
  37.         console.log(`Profit: ${profit} leva.`);
  38.     }
  39. }
  40.  
  41. excursionSales(["2", "2", "sea", "mountain", "sea", "sea", "mountain"]);
  42. excursionSales(["6", "3", "sea", "mountain", "mountain", "mountain", "sea", "Stop"]);
Add Comment
Please, Sign In to add comment