Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution() {
- let robot = {
- ingredients: { protein: 0, carbohydrate: 0, fat: 0, flavour: 0 },
- 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 }
- },
- restock(microelement, quantity) {
- this.ingredients[microelement] += quantity;
- return 'Success';
- },
- prepare(recipe, quantity) {
- let ingredientsCurrent = {};
- for(let key in this.recipes[recipe]) {
- let neededQuantity = this.recipes[recipe][key] * quantity;
- if(neededQuantity <= this.ingredients[key]) {
- ingredientsCurrent[key] = this.ingredients[key] - neededQuantity;
- } else {
- return `Error: not enough ${key} in stock`;
- }
- }
- Object.assign(this.ingredients, ingredientsCurrent);
- return 'Success';
- },
- report() {
- let result = '';
- for(let key in this.ingredients) {
- result += `${key}=${this.ingredients[key]} `;
- }
- result = result.trimEnd();
- return result;
- }
- };
- return (input) => {
- let [command, product, quantity] = input.split(' ');
- quantity = Number(quantity);
- return robot[command](product, quantity);
- };
- }
- let manager = solution ();
- console.log (manager ("prepare turkey 1")); // Error: not enough protein in stock
- console.log (manager ("restock protein 10")); // Success
- console.log (manager ("prepare turkey 1")); // Error: not enough carbohydrate in stock
- console.log (manager ("restock carbohydrate 10")); // Success
- console.log (manager ("prepare turkey 1")); // Error: not enough fat in stock
- console.log (manager ("restock fat 10")); // Success
- console.log (manager ("prepare turkey 1")); // Error: not enough flavour in stock
- console.log (manager ("restock flavour 10")); // Success
- console.log (manager ("prepare turkey 1")); // Success
- console.log (manager ("report")); // protein=0 carbohydrate=0 fat=0 flavour=0
Add Comment
Please, Sign In to add comment