Advertisement
Guest User

easterShop

a guest
Oct 19th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function easterShop(input) {
  2.     let index = 3;
  3.     let initialEggsInShop = Number(input[0]);
  4.     let command = input[1];
  5.     let eggsToFill = Number(input[2]);
  6.  
  7.     let eggsSold = 0;
  8.  
  9.     while (command !== "Close") {
  10.         if (command === "Fill") {
  11.             initialEggsInShop += eggsToFill;
  12.         }
  13.         else if (command === "Buy") {
  14.             initialEggsInShop -= eggsToFill;
  15.             eggsSold += eggsToFill
  16.         }
  17.  
  18.         command = input[index];
  19.         index++
  20.  
  21.         if (index >= input.length) {
  22.             break;
  23.         }
  24.  
  25.         eggsToFill = Number(input[index]);
  26.         index++;
  27.  
  28.         if (eggsToFill > initialEggsInShop) {
  29.             console.log(`Not enough eggs in store!`);
  30.             console.log(`You can buy only ${initialEggsInShop}.`);
  31.         }
  32.     }
  33.  
  34.     if(command === "Close") {
  35.         console.log(`Store is closed!`);
  36.         console.log(`${eggsSold} eggs sold.`);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement