Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const robot = (function result() {
- const ingredients = { protein: 0, carbohydrate: 0, fat: 0, flavour: 0 }
- const recipes = {
- 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 }
- }
- const checkIngredients = (recipes, meal, quantity, ingredients) => {
- return neededIngredients = Object.keys(recipes[meal]).reduce((neededIngredients, ingredient, ind) => {
- if (neededIngredients.thereIsEnoughtIngredients && recipes[meal][ingredient] * quantity > ingredients[ingredient]) {
- neededIngredients.thereIsEnoughtIngredients = false;
- neededIngredients.ingredientWhichIsNotEnought = ingredient;
- } else {
- neededIngredients[ingredient] = recipes[meal][ingredient] * quantity;
- }
- return neededIngredients
- }, { thereIsEnoughtIngredients: true, ingredientWhichIsNotEnought: '' });
- }
- const reduceIngredients = (recipes, meal, neededIngredients, ingredients) => {
- if (neededIngredients.thereIsEnoughtIngredients)
- Object.keys(recipes[meal]).forEach(ingredient => ingredients[ingredient] -= neededIngredients[ingredient])
- }
- return function solve(string) {
- const tokens = string.split(' ');
- const command = tokens[0];
- if (command === 'restock') {
- ingredients[tokens[1]] += Number(tokens[2]);
- return 'Success';
- } else if (command === 'prepare') {
- const meal = tokens[1];
- const quantity = Number(tokens[2]);
- checkIngredients(recipes, meal, quantity, ingredients);
- reduceIngredients(recipes, meal, neededIngredients, ingredients);
- return (neededIngredients.thereIsEnoughtIngredients)
- ? 'Success'
- : `Error: not enough ${neededIngredients.ingredientWhichIsNotEnought} in stock`
- } else if (command === 'report') {
- return `protein=${ingredients.protein} carbohydrate=${ingredients.carbohydrate} fat=${ingredients.fat} flavour=${ingredients.flavour}`;
- }
- }
- })();
- console.log(robot("restock carbohydrate 10"));
- console.log(robot("restock flavour 10"));
- console.log(robot("prepare apple 1"));
- console.log(robot("restock fat 10"));
- console.log(robot("prepare burger 1"));
- console.log(robot("report"));
- // Result is:
- // Success
- // Success
- // Success
- // Success
- // Success
- // protein=0 carbohydrate=4 fat=3 flavour=5
- console.log(robot("prepare turkey 1"));
- console.log(robot("restock protein 10"));
- console.log(robot("prepare turkey 1"));
- console.log(robot("restock carbohydrate 10"));
- console.log(robot("prepare turkey 1"));
- console.log(robot("restock fat 10"));
- console.log(robot("prepare turkey 1"));
- console.log(robot("restock flavour 10"));
- console.log(robot("prepare turkey 1"));
- console.log(robot("report"));
- // Result is:
- // Error: not enough protein in stock
- // Success
- // Error: not enough carbohydrate in stock
- // Success
- // Error: not enough fat in stock
- // Success
- // Error: not enough flavour in stock
- // Success
- // Success
- // protein=0 carbohydrate=0 fat=0 flavour=0
Advertisement
Add Comment
Please, Sign In to add comment