TZinovieva

Easter Shop

Dec 24th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function easterShop(input) {
  2.     let totalEggs = Number(input[0]);
  3.     let index = 1;
  4.     let command = input[index];
  5.     index++;
  6.  
  7.     let soldEggs = 0;
  8.     while (command !== "Close") {
  9.         let eggs = Number(input[index]);
  10.         index++;
  11.  
  12.         if (command === "Buy" && eggs > totalEggs) {
  13.             console.log(`Not enough eggs in store!`);
  14.             console.log(`You can buy only ${totalEggs}.`);
  15.             return;
  16.         }
  17.         if (command === "Buy") {
  18.             totalEggs -= eggs;
  19.             soldEggs += eggs;
  20.         } else {
  21.             totalEggs += eggs;
  22.         }
  23.         command = input[index];
  24.         index++;
  25.     }
  26.     if (command === "Close") {
  27.     console.log(`Store is closed!`);
  28.     console.log(`${soldEggs} eggs sold.`);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment