mihalkoff

Breakfast Robot

Feb 7th, 2022 (edited)
1,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.     let robot = {
  3.         ingredients: { protein: 0, carbohydrate: 0, fat: 0, flavour: 0 },
  4.         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.         restock(microelement, quantity) {
  12.             this.ingredients[microelement] += quantity;
  13.             return 'Success';
  14.         },
  15.         prepare(recipe, quantity) {
  16.             let ingredientsCurrent = {};
  17.  
  18.             for(let key in this.recipes[recipe]) {
  19.                 let neededQuantity = this.recipes[recipe][key] * quantity;
  20.  
  21.                 if(neededQuantity <= this.ingredients[key]) {
  22.                     ingredientsCurrent[key] = this.ingredients[key] - neededQuantity;
  23.                 } else {
  24.                     return `Error: not enough ${key} in stock`;
  25.                 }
  26.             }
  27.  
  28.             Object.assign(this.ingredients, ingredientsCurrent);
  29.  
  30.             return 'Success';
  31.         },
  32.         report() {
  33.             let result = '';
  34.             for(let key in this.ingredients) {
  35.                 result += `${key}=${this.ingredients[key]} `;
  36.             }
  37.  
  38.             result = result.trimEnd();
  39.             return result;
  40.         }
  41.     };
  42.  
  43.     return (input) => {
  44.         let [command, product, quantity] = input.split(' ');
  45.         quantity = Number(quantity);
  46.         return robot[command](product, quantity);
  47.     };
  48. }
  49.  
  50. let manager = solution ();
  51. console.log (manager ("prepare turkey 1")); // Error: not enough protein in stock
  52. console.log (manager ("restock protein 10")); // Success
  53. console.log (manager ("prepare turkey 1")); // Error: not enough carbohydrate in stock
  54. console.log (manager ("restock carbohydrate 10")); // Success
  55. console.log (manager ("prepare turkey 1")); // Error: not enough fat in stock
  56. console.log (manager ("restock fat 10")); // Success
  57. console.log (manager ("prepare turkey 1")); // Error: not enough flavour in stock
  58. console.log (manager ("restock flavour 10")); // Success
  59. console.log (manager ("prepare turkey 1")); // Success
  60. console.log (manager ("report")); // protein=0 carbohydrate=0 fat=0 flavour=0
Add Comment
Please, Sign In to add comment