Advertisement
Spocoman

Excursion Sale

Feb 20th, 2022 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. РЕШЕНИЕ С FOR:
  2.  
  3. function excursionSale(input) {
  4.     let seaCount = Number(input[0]);
  5.     let mountCount = Number(input[1]);
  6.     let totalSum = 0;
  7.  
  8.     for (let i = 2; i < input.length; i++) {
  9.         let command = input[i];
  10.         if (command === "Stop") {
  11.             break;
  12.         } else if (command === "sea") {
  13.             if (seaCount !== 0) {
  14.                 totalSum += 680;
  15.                 seaCount--;
  16.             }
  17.         } else if (command === "mountain") {
  18.             if (mountCount !== 0) {
  19.                 totalSum += 499;
  20.                 mountCount--;
  21.             }
  22.         }
  23.         if (seaCount === 0 && mountCount === 0) {
  24.             console.log(" Good job! Everything is sold.");
  25.             break;
  26.         }
  27.     }
  28.     console.log(`Profit: ${totalSum} leva.`);
  29. }
  30.  
  31. РЕШЕНИЕ СЪС SHIFT():
  32.  
  33. function excursionSale(input) {
  34.     let seaCount = Number(input.shift());
  35.     let mountCount = Number(input.shift());
  36.     let totalSum = 0;
  37.  
  38.     while (true) {
  39.         let command = input.shift();
  40.         if (command === "Stop") {
  41.             break;
  42.         } else if (command === "sea") {
  43.             if (seaCount !== 0) {
  44.                 totalSum += 680;
  45.                 seaCount--;
  46.             }
  47.         } else if (command === "mountain") {
  48.             if (mountCount !== 0) {
  49.                 totalSum += 499;
  50.                 mountCount--;
  51.             }
  52.         }
  53.         if (seaCount === 0 && mountCount === 0) {
  54.             console.log(" Good job! Everything is sold.");
  55.             break;
  56.         }
  57.     }
  58.     console.log(`Profit: ${totalSum} leva.`);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement