Advertisement
ckeuta

Untitled

Mar 14th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. function solve(input) {
  2. let eggsInShop = Number(input.shift());
  3. let command = input.shift();
  4. let buyOrFill = 0;
  5. while (command === 'Buy' || command === 'Fill') {
  6.  
  7. if (command === 'Buy') {
  8. buyOrFill = Number(input.shift());
  9.  
  10. if (eggsInShop < buyOrFill) {
  11. console.log(`Not enough eggs in store!`)
  12. console.log(`You can buy only ${eggsInShop}.`)
  13. break;
  14. }
  15. eggsInShop -= buyOrFill
  16. command = input.shift();
  17.  
  18.  
  19.  
  20.  
  21. } else if (command === 'Fill') {
  22. buyOrFill = Number(input.shift());
  23.  
  24. eggsInShop += buyOrFill
  25. command = input.shift();
  26.  
  27. }
  28.  
  29.  
  30. }
  31.  
  32. if (command === 'Close') {
  33.  
  34. console.log(`Store is closed!`)
  35. console.log(`${eggsInShop + buyOrFill} eggs sold.`)
  36.  
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. }
  45.  
  46. solve(
  47.  
  48. [
  49. '13', 'Buy',
  50. '8', 'Fill',
  51. '3', 'Buy',
  52. '10'
  53. ]
  54.  
  55.  
  56.  
  57. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement