bobo_bobkata

Untitled

Oct 8th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. func = () => {
  2. const ing = {
  3. protein: 0,
  4. carbohydrate: 0,
  5. fat: 0,
  6. flavour: 0,
  7. };
  8.  
  9. const recipes = {
  10. apple: { carbohydrate: 1, flavour: 2 },
  11. lemonade: { carbohydrate: 10, flavour: 20 },
  12. burger: { carbohydrate: 5, fat: 7, flavour: 3 },
  13. turkey: { protein: 10, carbohydrate: 10, fat: 10, flavour: 10 },
  14. eggs: { protein: 5, fat: 1, flavour: 1 },
  15. };
  16.  
  17. commands = {
  18. restock: (x, y) => {
  19. ing[x] += Number(y);
  20. return 'Success';
  21. },
  22.  
  23. prepare: (x, y) => {
  24. let result = 'Success';
  25. let success = true;
  26. recipe = Object.entries(recipes[x]);
  27.  
  28. for (let elem of recipe) {
  29. if (ing[elem[0]] < elem[1] * y) {
  30. success = false;
  31. result = `Error: not enough ${elem[0]} in stock`;
  32. break;
  33. }
  34. }
  35.  
  36. if (success) {
  37. recipe.forEach(e => (ing[e[0]] -= e[1] * y));
  38. }
  39. return result;
  40. },
  41.  
  42. report: () =>
  43. Object.entries(ing)
  44. .map(e => `${e[0]}=${e[1]}`)
  45. .join(' '),
  46. };
  47.  
  48. return x => {
  49. const [command, element, quantity] = x.split(' ');
  50. return commands[command](element, quantity);
  51. };
  52. }
Add Comment
Please, Sign In to add comment