Spocoman

Easter Shop

Feb 21st, 2022 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function easterShop(input) {
  2.     let availableEggs = Number(input[0]);
  3.     let soldEggs = 0;
  4.     let index = 1;
  5.  
  6.     let command = input[index++];
  7.     while (command !== "Close") {
  8.         let eggs = Number(input[index++]);
  9.         if (command === "Buy") {
  10.             if (availableEggs - eggs < 0) {
  11.                 break;
  12.             }
  13.             availableEggs -= eggs;
  14.             soldEggs += eggs;
  15.         } else if (command === "Fill") {
  16.             availableEggs += eggs;
  17.         }
  18.         command = input[index++];
  19.     }
  20.     if (command === "Close") {
  21.         console.log("Store is closed!");
  22.         console.log(`${soldEggs} eggs sold.`);
  23.     } else {
  24.         console.log("Not enough eggs in store!");
  25.         console.log(`You can buy only ${availableEggs}.`);
  26.     }
  27. }
  28.  
  29.  
  30. Решение със shift:
  31.  
  32. function easterShop(input) {
  33.     let availableEggs = Number(input.shift());
  34.     let soldEggs = 0;
  35.  
  36.     let command = input.shift();
  37.     while (command !== "Close") {
  38.         let eggs = Number(input.shift());
  39.         if (command === "Buy") {
  40.             if (availableEggs - eggs < 0) {
  41.                 break;
  42.             }
  43.             availableEggs -= eggs;
  44.             soldEggs += eggs;
  45.         } else if (command === "Fill") {
  46.             availableEggs += eggs;
  47.         }
  48.         command = input.shift();
  49.     }
  50.     if (command === "Close") {
  51.         console.log("Store is closed!");
  52.         console.log(`${soldEggs} eggs sold.`);
  53.     } else {
  54.         console.log("Not enough eggs in store!");
  55.         console.log(`You can buy only ${availableEggs}.`);
  56.     }
  57. }
Add Comment
Please, Sign In to add comment