Advertisement
Mvelchev

Untitled

Aug 15th, 2022
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bakeryShop(input) {
  2.         let products = new Map();
  3.        
  4.  
  5.       for (let line of input){
  6.         //   console.log(line); OK
  7.             let tokens = line.split(' ');
  8.             //console.log(tokens); OK
  9.             let command = tokens[0];
  10.             let quantity = Number(tokens[1]);
  11.             let food = tokens[2];
  12.             //console.log(food); OK
  13.            
  14.             while (command !== 'Complete'){
  15.                     if (command === 'Receive'){
  16.                         if (quantity < 0){
  17.                             break;
  18.                         }
  19.                         else if (!products.has(food)){
  20.                             products.set(food, quantity);
  21.                             break;
  22.                         }  else if (products.has(food)){
  23.                             let oldValue = Number(products.get(food));
  24.                             let newValue = oldValue + quantity;
  25.                             products.set(food, newValue);
  26.                         }
  27.                     } else if ( command === 'Sell'){
  28.                         if (!products.has(food)){
  29.                             console.log(`You don't have any ${food}`);
  30.                        } else if (products.has(food)){
  31.                            if (products.food(quantity) < quantity){
  32.                                products.delete(food);
  33.                                console.log(`There aren't enough ${food}. You sold the last ${quantity} of them`)
  34.                             } else if (products.food(quantity) >= quantity){
  35.                                 products.set(quantity - quantity);
  36.                                 if (products.food(quantity) === 0){
  37.                                     products.delete(food);
  38.                                 }
  39.                             }
  40.                         }
  41.                     }
  42.                    
  43.             }
  44.            
  45.       }
  46.       console.log(products);
  47.      
  48. }
  49. bakeryShop(["Receive 105 cookies",
  50.             "Receive 10 donuts",
  51.             "Sell 10 donuts",
  52.             "Sell 1 bread",
  53.             "Complete"]);
  54. console.log('--------------------------------------------');
  55. bakeryShop(["Receive 10 muffins",
  56.             "Receive 23 bagels",
  57.             "Sell 5 muffins",
  58.             "Sell 10 bagels",
  59.             "Complete"])
  60. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement