Advertisement
Liliana797979

breakfast robot - js advanced

Oct 1st, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.  
  3.   let supplies = { protein: 0, carbohydrate: 0, fat: 0, flavour: 0 }
  4.  
  5.   let foodRecipes = {
  6.     apple: { protein: 0, carbohydrate: 1, fat: 0, flavour: 2 },
  7.     lemonade: { protein: 0, carbohydrate: 10, fat: 0, flavour: 20 },
  8.     burger: { protein: 0, carbohydrate: 5, fat: 7, flavour: 3 },
  9.     eggs: { protein: 5, carbohydrate: 0, fat: 1, flavour: 1 },
  10.     turkey: { protein: 10, carbohydrate: 10, fat: 10, flavour: 10 },
  11.     cookRecipe(recipe, quantity) {
  12.  
  13.       let deductedQuantity = {};
  14.  
  15.       for (const value in this[recipe]) {
  16.         if (this[recipe][value] * quantity > supplies[value]) {
  17.           return `Error: not enough ${value} in stock`;
  18.         }
  19.         deductedQuantity[value] = supplies[value] - this[recipe][value] * quantity;
  20.       }
  21.  
  22.       Object.assign(supplies, deductedQuantity);
  23.       return 'Success';
  24.  
  25.     }
  26.   }
  27.  
  28.   return controller = (str) => {
  29.  
  30.     if (str.includes('restock')) {
  31.       let [, nutrient, quantity] = str.split(' ');
  32.       supplies[nutrient] += Number(quantity);
  33.       return 'Success';
  34.     }
  35.  
  36.     if (str.includes('prepare')) {
  37.       let [,  recipe, quantity] = str.split(' ');
  38.       return foodRecipes.cookRecipe(recipe, Number(quantity));
  39.     }
  40.  
  41.     return `protein=${supplies.protein} carbohydrate=${supplies.carbohydrate} fat=${supplies.fat} flavour=${supplies.flavour}`;
  42.  
  43.   }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement