Todorov_Stanimir

06. Breakfast Robot Exercise: Advanced Functions

Oct 2nd, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const robot = (function result() {
  2.     const ingredients = { protein: 0, carbohydrate: 0, fat: 0, flavour: 0 }
  3.  
  4.     const recipes = {
  5.         apple: { carbohydrate: 1, flavour: 2 },
  6.         lemonade: { carbohydrate: 10, flavour: 20 },
  7.         burger: { carbohydrate: 5, fat: 7, flavour: 3 },
  8.         eggs: { protein: 5, fat: 1, flavour: 1 },
  9.         turkey: { protein: 10, carbohydrate: 10, fat: 10, flavour: 10 }
  10.     }
  11.  
  12.     const checkIngredients = (recipes, meal, quantity, ingredients) => {
  13.         return neededIngredients = Object.keys(recipes[meal]).reduce((neededIngredients, ingredient, ind) => {
  14.             if (neededIngredients.thereIsEnoughtIngredients && recipes[meal][ingredient] * quantity > ingredients[ingredient]) {
  15.                 neededIngredients.thereIsEnoughtIngredients = false;
  16.                 neededIngredients.ingredientWhichIsNotEnought = ingredient;
  17.             } else {
  18.                 neededIngredients[ingredient] = recipes[meal][ingredient] * quantity;
  19.             }
  20.             return neededIngredients
  21.         }, { thereIsEnoughtIngredients: true, ingredientWhichIsNotEnought: '' });
  22.     }
  23.  
  24.     const reduceIngredients = (recipes, meal, neededIngredients, ingredients) => {
  25.         if (neededIngredients.thereIsEnoughtIngredients)
  26.             Object.keys(recipes[meal]).forEach(ingredient => ingredients[ingredient] -= neededIngredients[ingredient])
  27.     }
  28.  
  29.     return function solve(string) {
  30.         const tokens = string.split(' ');
  31.         const command = tokens[0];
  32.  
  33.         if (command === 'restock') {
  34.             ingredients[tokens[1]] += Number(tokens[2]);
  35.             return 'Success';
  36.         } else if (command === 'prepare') {
  37.             const meal = tokens[1];
  38.             const quantity = Number(tokens[2]);
  39.             checkIngredients(recipes, meal, quantity, ingredients);
  40.             reduceIngredients(recipes, meal, neededIngredients, ingredients);
  41.  
  42.             return (neededIngredients.thereIsEnoughtIngredients)
  43.                 ? 'Success'
  44.                 : `Error: not enough ${neededIngredients.ingredientWhichIsNotEnought} in stock`
  45.  
  46.         } else if (command === 'report') {
  47.             return `protein=${ingredients.protein} carbohydrate=${ingredients.carbohydrate} fat=${ingredients.fat} flavour=${ingredients.flavour}`;
  48.         }
  49.     }
  50. })();
  51.  
  52. console.log(robot("restock carbohydrate 10"));
  53. console.log(robot("restock flavour 10"));
  54. console.log(robot("prepare apple 1"));
  55. console.log(robot("restock fat 10"));
  56. console.log(robot("prepare burger 1"));
  57. console.log(robot("report"));
  58. // Result is:
  59. // Success
  60. // Success
  61. // Success
  62. // Success
  63. // Success
  64. // protein=0 carbohydrate=4 fat=3 flavour=5
  65.  
  66. console.log(robot("prepare turkey 1"));
  67. console.log(robot("restock protein 10"));
  68. console.log(robot("prepare turkey 1"));
  69. console.log(robot("restock carbohydrate 10"));
  70. console.log(robot("prepare turkey 1"));
  71. console.log(robot("restock fat 10"));
  72. console.log(robot("prepare turkey 1"));
  73. console.log(robot("restock flavour 10"));
  74. console.log(robot("prepare turkey 1"));
  75. console.log(robot("report"));
  76. // Result is:
  77. // Error: not enough protein in stock
  78. // Success
  79. // Error: not enough carbohydrate in stock
  80. // Success
  81. // Error: not enough fat in stock
  82. // Success
  83. // Error: not enough flavour in stock
  84. // Success
  85. // Success
  86. // protein=0 carbohydrate=0 fat=0 flavour=0
Advertisement
Add Comment
Please, Sign In to add comment