Advertisement
Guest User

Breakfast Robot

a guest
Jun 16th, 2020
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function () {
  2.     function restock(input) {
  3.         microelement = input[0].toLowerCase();
  4.         quantity = Number(input[1]);
  5.         microelements[microelement] += Number(quantity);
  6.         return 'Success';
  7.     }
  8.  
  9.     function prepare(input) {
  10.         let recipe = input[0].toLowerCase();
  11.         quantity = Number(input[1]);
  12.         let result = '';
  13.         if (quantity * recipes[recipe]['protein'] > microelements['protein']) {
  14.             result = 'Error: not enough protein in stock';
  15.         } else if (
  16.             quantity * recipes[recipe]['carbohydrate'] >
  17.             microelements['carbohydrate']
  18.         ) {
  19.             result = 'Error: not enough carbohydrate in stock';
  20.         } else if (quantity * recipes[recipe]['fat'] > microelements['fat']) {
  21.             result = 'Error: not enough fat in stock';
  22.         } else if (
  23.             quantity * recipes[recipe]['flavour'] >
  24.             microelements['flavour']
  25.         ) {
  26.             result = 'Error: not enough flavour in stock';
  27.         } else {
  28.             result = 'Success';
  29.             microelements['protein'] -= quantity * recipes[recipe]['protein'];
  30.             microelements['carbohydrate'] -=
  31.                 quantity * recipes[recipe]['carbohydrate'];
  32.             microelements['fat'] -= quantity * recipes[recipe]['fat'];
  33.             microelements['flavour'] -= quantity * recipes[recipe]['flavour'];
  34.         }
  35.         return result;
  36.     }
  37.  
  38.     function report() {
  39.         return `protein=${microelements['protein']} carbohydrate=${microelements['carbohydrate']} fat=${microelements['fat']} flavour=${microelements['flavour']}`;
  40.     }
  41.  
  42.     let microelements = {
  43.         protein: 0,
  44.         carbohydrate: 0,
  45.         fat: 0,
  46.         flavour: 0,
  47.     };
  48.  
  49.     let recipes = {
  50.         apple: { protein: 0, carbohydrate: 1, flavour: 2, fat: 0 },
  51.         coke: { protein: 0, carbohydrate: 10, flavour: 20, fat: 0 },
  52.         burger: { protein: 0, carbohydrate: 5, flavour: 3, fat: 7 },
  53.         omelet: { protein: 5, carbohydrate: 0, flavour: 1, fat: 1 },
  54.         cheverme: { protein: 10, carbohydrate: 10, flavour: 10, fat: 10 },
  55.     };
  56.  
  57.     return function (command) {
  58.         command = command.split(' ');
  59.         let action = command.shift();
  60.         switch (action) {
  61.             case 'prepare':
  62.                 return prepare(command);
  63.                 break;
  64.             case 'restock':
  65.                 return restock(command);
  66.                 break;
  67.             case 'report':
  68.                 return report();
  69.                 break;
  70.         }
  71.     };
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement