Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution() {
- const products = {
- protein: 0,
- carbohydrate: 0,
- fat: 0,
- flavour: 0
- }
- const meals = {
- apple: { carbohydrate: 1, flavour: 2 },
- lemonade: { carbohydrate: 10, flavour: 20 },
- burger: { carbohydrate: 5, fat: 7, flavour: 3 },
- eggs: { protein: 5, fat: 1, flavour: 1 },
- turkey: { protein: 10, carbohydrate: 10, fat: 10, flavour: 10 }
- }
- return function action (str) {
- let array=str.split(' ');
- let toDo = array[0];
- if (toDo === 'restock') {
- let product = array[1];
- let quantity = Number(array[2]);
- products[product] += quantity;
- return 'Success';
- } else if (toDo === 'prepare') {
- let meal = array[1];
- let quantity = Number(array[2]);
- for (let ingredient in meals[meal]) {
- const neededQuantity = meals[meal][ingredient] * quantity;
- if (neededQuantity > products[ingredient]) {
- return `Error: not enough ${ingredient} in stock`;
- } else {
- const neededQuantity = meals[meal][ingredient] * quantity;
- products[ingredient] -= neededQuantity;
- return 'Success';
- }
- }
- } else if (toDo === 'report') {
- return `protein=${products.protein} carbohydrate=${products.carbohydrate} fat=${products.fat} flavour=${products.flavour}`;
- }
- }
- }
- let rob = solution();
- console.log(rob ('restock flavour 50'));
- console.log(rob ('report'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement