Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution () {
- return (() => {
- let ingredients = {};
- ingredients['protein'] = 0;
- ingredients['carbohydrate'] = 0;
- ingredients['fat'] = 0;
- ingredients['flavour'] = 0;
- function restock ([microelement, quantity]) {
- quantity = +quantity;
- ingredients[microelement] += quantity
- return 'Success';
- }
- function prepare ([recipe, quantity]) {
- quantity = +quantity;
- let msg = '';
- switch (recipe) {
- case 'apple':
- if (ingredients['flavour'] < quantity * 2) {
- msg = "Error: not enough flavour in stock";
- }
- if (ingredients['carbohydrate'] < quantity) {
- msg = "Error: not enough carbohydrate in stock";
- } else {
- ingredients['flavour'] -= quantity * 2;
- ingredients['carbohydrate'] -= quantity;
- msg = "Success";
- }
- return msg;
- break;
- case 'lemonade':
- if (ingredients['carbohydrate'] < quantity * 10) {
- msg = "Error: not enough carbohydrate in stock";
- }
- if (ingredients['flavour'] < quantity * 20) {
- msg = "Error: not enough flavour in stock";
- } else {
- ingredients['flavour'] -= quantity * 20;
- ingredients['carbohydrate'] -= quantity * 10;
- msg = "Success";
- }
- return msg;
- break;
- case 'burger':
- if (ingredients['carbohydrate'] < quantity * 5) {
- msg = "Error: not enough flavour in stock";
- }
- if (ingredients['fat'] < quantity * 7) {
- msg = "Error: not enough fat in stock";
- }
- if (ingredients['flavour'] < quantity * 3) {
- msg = "Error: not enough flavour in stock";
- } else {
- ingredients['flavour'] -= quantity * 3;
- ingredients['fat'] -= quantity * 7;
- ingredients['carbohydrate'] -= quantity * 5;
- msg = 'Success'
- }
- return msg;
- break;
- case 'eggs':
- if (ingredients['protein'] < quantity * 5) {
- msg = "Error: not enough protein in stock";
- }
- if (ingredients['fat'] < quantity) {
- msg = "Error: not enough fat in stock";
- }
- if (ingredients['flavour'] < quantity) {
- msg = "Error: not enough flavour in stock";
- } else {
- ingredients['flavour'] -= quantity;
- ingredients['fat'] -= quantity;
- ingredients['protein'] -= quantity * 5;
- msg = 'Success'
- }
- return msg;
- break;
- case 'turkey':
- if (ingredients['protein'] < quantity * 10) {
- msg = "Error: not enough protein in stock";
- }
- if (ingredients['carbohydrate'] < quantity * 10) {
- msg = "Error: not enough carbohydrate in stock";
- }
- if (ingredients['fat'] < quantity * 10) {
- msg = "Error: not enough fat in stock";
- }
- if (ingredients['flavour'] < quantity * 10) {
- msg = "Error: not enough flavour in stock";
- } else {
- ingredients['protein'] -= quantity * 10;
- ingredients['fat'] -= quantity * 10;
- ingredients['carbohydrate'] -= quantity * 10;
- ingredients['flavour'] -= quantity * 10;
- msg = 'Success';
- }
- return msg;
- break;
- }
- }
- function report () {
- return `protein=${ingredients['protein']} carbohydrate=${ingredients['carbohydrate']} fat=${ingredients['fat']} flavour=${ingredients['flavour']}`;
- }
- return function (command) {
- let tokens = command.split(' ');
- let action = tokens.shift();
- switch (action) {
- case "prepare":
- return prepare(tokens);
- break;
- case "restock":
- return restock(tokens);
- break;
- case "report":
- return report();
- }
- }
- })();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement