Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function excursionSales(input) {
- let index = 0;
- let seaTrip = Number(input[index]);
- index++;
- let mountainTrip = Number(input[index]);
- index++;
- let command = input[index];
- index++;
- let profit = 0;
- let allTripsSold = false;
- while (command !== "Stop") {
- if (command === "sea") {
- if (seaTrip > 0) {
- seaTrip--;
- profit += 680;
- }
- } else if (command === "mountain") {
- if (mountainTrip > 0) {
- mountainTrip--;
- profit += 499;
- }
- }
- if (seaTrip === 0 && mountainTrip === 0) {
- allTripsSold = true;
- console.log("Good job! Everything is sold.");
- console.log(`Profit: ${profit} leva.`);
- break;
- }
- command = input[index];
- index++;
- }
- if (!allTripsSold) {
- console.log(`Profit: ${profit} leva.`);
- }
- }
- excursionSales(["2", "2", "sea", "mountain", "sea", "sea", "mountain"]);
- excursionSales(["6", "3", "sea", "mountain", "mountain", "mountain", "sea", "Stop"]);
Add Comment
Please, Sign In to add comment