Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. function solution() {
  2. const products = {
  3. protein: 0,
  4. carbohydrate: 0,
  5. fat: 0,
  6. flavour: 0
  7. }
  8. const meals = {
  9. apple: { carbohydrate: 1, flavour: 2 },
  10. lemonade: { carbohydrate: 10, flavour: 20 },
  11. burger: { carbohydrate: 5, fat: 7, flavour: 3 },
  12. eggs: { protein: 5, fat: 1, flavour: 1 },
  13. turkey: { protein: 10, carbohydrate: 10, fat: 10, flavour: 10 }
  14. }
  15.  
  16. return function action (str) {
  17. let array=str.split(' ');
  18. let toDo = array[0];
  19.  
  20. if (toDo === 'restock') {
  21. let product = array[1];
  22. let quantity = Number(array[2]);
  23. products[product] += quantity;
  24. return 'Success';
  25. } else if (toDo === 'prepare') {
  26. let meal = array[1];
  27. let quantity = Number(array[2]);
  28. for (let ingredient in meals[meal]) {
  29. const neededQuantity = meals[meal][ingredient] * quantity;
  30. if (neededQuantity > products[ingredient]) {
  31. return `Error: not enough ${ingredient} in stock`;
  32. } else {
  33. const neededQuantity = meals[meal][ingredient] * quantity;
  34. products[ingredient] -= neededQuantity;
  35. return 'Success';
  36. }
  37. }
  38. } else if (toDo === 'report') {
  39. return `protein=${products.protein} carbohydrate=${products.carbohydrate} fat=${products.fat} flavour=${products.flavour}`;
  40. }
  41. }
  42. }
  43. let rob = solution();
  44. console.log(rob ('restock flavour 50'));
  45. console.log(rob ('report'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement