Advertisement
geromero

Untitled

Feb 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. (function () {
  2. let recipies = {
  3. coke:{carbohydrate:10,flavour:20},
  4. apple:{carbohydrate:1,flavour:2},
  5. burger:{carbohydrate:5,fat:7,flavour:3},
  6. omelet:{protein:5,fat:1,flavour:1},
  7. cheverme:{protein:10,carbohydrate:10,fat:10,flavour:10}
  8. }
  9.  
  10. let availableIngredients = {protein:0,carbohydrate:0,fat:0,flavour:0};
  11. return function cookOrStore(commandInput) {
  12. let tokens = commandInput.split(' ');
  13.  
  14. let command = tokens[0]
  15. if(command==='restock'){
  16. let ingredient = tokens[1];
  17. let quantity = Number(tokens[2])
  18. availableIngredients[ingredient]+=quantity;
  19. console.log('Success')
  20. }
  21. else if(command==='prepare'){
  22. let meal = tokens[1];
  23. let quantity = tokens[2];
  24. let ingredients = Array.from(Object.keys(recipies[meal]))
  25. let isSuccessful = true;
  26. for(let i = 0;i<ingredients.length;i++) {
  27. let neededQuantity = recipies[meal][ingredients[i]] * quantity
  28. if (neededQuantity > availableIngredients[ingredients[i]]) {
  29. console.log(`Error: not enough ${ingredients[i]} in stock`);
  30. isSuccessful = false;
  31. break;
  32. }
  33. }
  34. if(isSuccessful){
  35.  
  36. console.log('Success')
  37. ingredients.forEach(ingredient=>{
  38. let neededQuantity = recipies[meal][ingredient] * quantity
  39. availableIngredients[ingredient]-=neededQuantity
  40. })
  41. }
  42. }
  43. else if(command==='report'){
  44. let resultString = ''
  45. for (let obj in availableIngredients) {
  46. resultString+=`${obj}=${availableIngredients[obj]} `
  47. }
  48. console.log(resultString)
  49. }
  50. }
  51. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement