Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bakeryShop(input) {
- let products = new Map();
- for (let line of input){
- // console.log(line); OK
- let tokens = line.split(' ');
- //console.log(tokens); OK
- let command = tokens[0];
- let quantity = Number(tokens[1]);
- let food = tokens[2];
- //console.log(food); OK
- while (command !== 'Complete'){
- if (command === 'Receive'){
- if (quantity < 0){
- break;
- }
- else if (!products.has(food)){
- products.set(food, quantity);
- break;
- } else if (products.has(food)){
- let oldValue = Number(products.get(food));
- let newValue = oldValue + quantity;
- products.set(food, newValue);
- }
- } else if ( command === 'Sell'){
- if (!products.has(food)){
- console.log(`You don't have any ${food}`);
- } else if (products.has(food)){
- if (products.food(quantity) < quantity){
- products.delete(food);
- console.log(`There aren't enough ${food}. You sold the last ${quantity} of them`)
- } else if (products.food(quantity) >= quantity){
- products.set(quantity - quantity);
- if (products.food(quantity) === 0){
- products.delete(food);
- }
- }
- }
- }
- }
- }
- console.log(products);
- }
- bakeryShop(["Receive 105 cookies",
- "Receive 10 donuts",
- "Sell 10 donuts",
- "Sell 1 bread",
- "Complete"]);
- console.log('--------------------------------------------');
- bakeryShop(["Receive 10 muffins",
- "Receive 23 bagels",
- "Sell 5 muffins",
- "Sell 10 bagels",
- "Complete"])
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement