Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution() {
- let storage = {
- protein: 0,
- carbohydrate: 0,
- fat: 0,
- flavour: 0,
- }
- let foodInfo = {
- 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,
- }
- }
- let handler = {
- restock(microelement, quantity) {
- storage[microelement] += quantity;
- return "Success"
- },
- prepare(recipe, quantity) {
- let requiredAmmountPerUnit = foodInfo[recipe];
- let items = Object.entries(requiredAmmountPerUnit);
- let canCook = true;
- for (const pair of items) {
- let ingredient = pair[0];
- let ammount = pair[1];
- if (storage[ingredient] < ammount * quantity) {
- canCook = false;
- return `Error: not enough ${ingredient} in stock`
- }
- }
- if (canCook) {
- items.forEach(x => storage[x[0]] -= x[1] * quantity);
- return `Success`;
- }
- },
- report() {
- let entries = Object.entries(storage);
- return `${entries[0][0]}=${entries[0][1]} ${entries[1][0]}=${entries[1][1]} ${entries[2][0]}=${entries[2][1]} ${entries[3][0]}=${entries[3][1]}`
- }
- }
- return function management(param) {
- let [action, token, quantity] = param.split(' ');
- quantity = +quantity;
- if (action !== 'report') {
- return handler[action](token, quantity);
- } else {
- return handler[action]();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment