Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution() {
- const stock = {
- protein: 0,
- carbohydrate: 0,
- fat: 0,
- flavour: 0,
- };
- const receipts = {
- 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,
- },
- };
- function report() {
- const result = Object.keys(stock).reduce((acc, element) => {
- acc.push(`${element}=${stock[element]}`);
- return acc;
- }, []);
- return result.join(' ');
- }
- function restock(args) {
- const [, product, qtty] = args;
- stock[product] += +qtty;
- return 'Success';
- }
- function prepare(args) {
- const [, food, qtty] = args;
- let canPrepare = true;
- const currentReceipt = Object.entries(receipts[food]);
- for (const element of currentReceipt) {
- const [ingredient, qt] = element;
- if (stock[ingredient] < +qt * +qtty) {
- canPrepare = false;
- return `Error: not enough ${ingredient} in stock`;
- }
- }
- if (canPrepare) {
- currentReceipt.forEach((element) => {
- const [ingredient, qt] = element;
- stock[ingredient] -= +qt * +qtty;
- });
- return 'Success';
- }
- }
- function finalResult(input) {
- const args = input.split(' ');
- if (args[0] === 'report') {
- return report();
- }
- if (args[0] === 'restock') {
- return restock(args);
- }
- if (args[0] === 'prepare') {
- return prepare(args);
- }
- }
- return finalResult;
- }
Advertisement
Add Comment
Please, Sign In to add comment