Advertisement
Guest User

Untitled

a guest
Jun 17th, 2021
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function foodRobot() {
  2.     const recipes = {
  3.         apple: { carbohydrate: 1, flavour: 2, },
  4.         lemonade: { carbohydrate: 10, flavour: 20, },
  5.         burger: { carbohydrate: 5, fat: 7, flavour: 3, },
  6.         eggs: { protein: 5, fat: 1, flavour: 1, },
  7.         turkey: { protein: 10, carbohydrate: 10, fat: 10, flavour: 10, }
  8.     }
  9.  
  10.     const productStorage = {
  11.         protein: 0,
  12.         carbohydrate: 0,
  13.         fat: 0,
  14.         flavour: 0
  15.     }
  16.  
  17.     let output = '';
  18.  
  19.     const actions = {
  20.         restock: (macronutrient, quantity) => {
  21.             productStorage[macronutrient] += quantity;
  22.             output = 'Success'
  23.         },
  24.         prepare: (recipe, quantity) => {
  25.             let enoughProducts = true;
  26.  
  27.             Object.entries(recipes[recipe]).forEach(entry => {
  28.                 let [macronutrient, count] = entry;
  29.  
  30.                 if (enoughProducts && productStorage[macronutrient] < (count * Number(quantity))) {
  31.                     enoughProducts = false;
  32.                     output = `Error: not enough ${macronutrient} in stock`;
  33.                 }
  34.             })
  35.  
  36.             if (enoughProducts) {
  37.                 Object.entries(recipes[recipe]).forEach(entry => {
  38.                     let [macronutrient, count] = entry;
  39.    
  40.                     productStorage[macronutrient] -= count*quantity;
  41.                 })
  42.  
  43.                 output = 'Success';
  44.             }
  45.         },
  46.         report: () => {
  47.             output = '';
  48.  
  49.             Object.entries(productStorage).forEach(entry => {
  50.                 let [macronutrient, quantity] = entry;
  51.  
  52.                 output += `${macronutrient}=${quantity} `;
  53.             })
  54.             output = output.trimEnd();
  55.         }    
  56.     }
  57.  
  58.     return function(input) {
  59.         let [command, product, quantity] = input.split(' ');
  60.         quantity = Number(quantity);
  61.  
  62.         actions[command](product, quantity);
  63.  
  64.         return output;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement