Advertisement
viligen

robotsBreakfast

Jun 6th, 2022
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.     const ingredients = {
  3.         protein: 0,
  4.         carbohydrate: 0,
  5.         fat: 0,
  6.         flavour: 0,
  7.     };
  8.  
  9.     const recipe = {
  10.         apple: { carbohydrate: 1, flavour: 2 },
  11.         lemonade: { carbohydrate: 10, flavour: 20 },
  12.         burger: { carbohydrate: 5, fat: 7, flavour: 3 },
  13.         eggs: { protein: 5, fat: 1, flavour: 1 },
  14.         turkey: { protein: 10, carbohydrate: 10, fat: 10, flavour: 10 },
  15.     };
  16.  
  17.     const commands = {
  18.         restock: (elem, quantity) => {
  19.             ingredients[elem] += Number(quantity);
  20.             return "Success";
  21.         },
  22.         prepare: (product, quantity) => {
  23.             for (el in recipe[product]) {
  24.                 if (recipe[product][el] * Number(quantity) > ingredients[el]) {
  25.                     return `Error: not enough ${el} in stock`;
  26.                 }
  27.             }
  28.             for (el in recipe[product]) {
  29.                 ingredients[el] -= recipe[product][el] * Number(quantity);
  30.             }
  31.             return "Success";
  32.         },
  33.         report: () => {
  34.             return `protein=${ingredients.protein} carbohydrate=${ingredients.carbohydrate} fat=${ingredients.fat} flavour=${ingredients.flavour}`;
  35.         },
  36.     };
  37.  
  38.     function manager(text) {
  39.         let [com, ...args] = text.split(" ");
  40.  
  41.         return commands[com](...args);
  42.     }
  43.  
  44.     return manager;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement