Advertisement
Guest User

Breakfast Robot - SoftUni JS Advanced

a guest
Jun 23rd, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.  
  3.     let robotIngridients = {
  4.  
  5.         'protein': 0,
  6.         'carbohydrate': 0,
  7.         'fat': 0,
  8.         'flavour': 0
  9.  
  10.     };
  11.  
  12.     function restock([element, quantity]) {
  13.  
  14.         robotIngridients[element] += Number(quantity);
  15.         return "Success";
  16.  
  17.     }
  18.  
  19.     function prepare([recipe, quantity]) {
  20.  
  21.         quantity = Number(quantity);
  22.         switch (recipe) {
  23.             case 'apple':
  24.                 if (robotIngridients['carbohydrate'] < 1 * quantity) {
  25.                     return "Error: not enough carbohydrate in stock"
  26.                 }
  27.                 else if (robotIngridients['flavour'] < 2 * quantity) {
  28.                     return "Error: not enough flavour in stock"
  29.                 }
  30.                 else {
  31.                     robotIngridients['carbohydrate'] -= 1 * quantity;
  32.                     robotIngridients['flavour'] -= 2 * quantity;
  33.                     return "Success"
  34.                 }
  35.  
  36.             case 'coke':
  37.                 if (robotIngridients['carbohydrate'] < 10 * quantity) {
  38.                     return "Error: not enough carbohydrate in stock"
  39.                 }
  40.                 else if (robotIngridients['flavour'] < 20 * quantity) {
  41.                     return "Error: not enough flavour in stock"
  42.                 }
  43.                 else {
  44.                     robotIngridients['carbohydrate'] -= 10 * quantity;
  45.                     robotIngridients['flavour'] -= 20 * quantity;
  46.                     return "Success"
  47.                 }
  48.  
  49.             case 'burger':
  50.                 if (robotIngridients['carbohydrate'] < 5 * quantity) {
  51.                     return "Error: not enough carbohydrate in stock"
  52.                 }
  53.                 else if (robotIngridients['fat'] < 7 * quantity) {
  54.                     return "Error: not enough fat in stock"
  55.                 }
  56.                 else if (robotIngridients['flavour'] < 3 * quantity) {
  57.                     return "Error: not enough flavour in stock"
  58.                 }
  59.                 else {
  60.                     robotIngridients['carbohydrate'] -= 5 * quantity;
  61.                     robotIngridients['fat'] -= 7 * quantity;
  62.                     robotIngridients['flavour'] -= 3 * quantity;
  63.                     return "Success"
  64.                 }
  65.  
  66.             case 'omelet':
  67.                 if (robotIngridients['protein'] < 5 * quantity) {
  68.                     return "Error: not enough protein in stock"
  69.                 }
  70.                 else if (robotIngridients['fat'] < 1 * quantity) {
  71.                     return "Error: not enough fat in stock"
  72.                 }
  73.                 else if (robotIngridients['flavour'] < 1 * quantity) {
  74.                     return "Error: not enough flavour in stock"
  75.                 }
  76.                 else {
  77.                     robotIngridients['protein'] -= 5 * quantity;
  78.                     robotIngridients['fat'] -= 1 * quantity;
  79.                     robotIngridients['flavour'] -= 1 * quantity;
  80.                     return "Success"
  81.                 }
  82.  
  83.             case 'cheverme':
  84.  
  85.                 if (robotIngridients['protein'] < 10 * quantity) {
  86.                     return "Error: not enough protein in stock"
  87.                 }
  88.                 else if (robotIngridients['carbohydrate'] < 10 * quantity) {
  89.                     return "Error: not enough carbohydrate in stock"
  90.                 }
  91.                 else if (robotIngridients['fat'] < 10 * quantity) {
  92.                     return "Error: not enough fat in stock"
  93.                 }
  94.                 else if (robotIngridients['flavour'] < 10 * quantity) {
  95.                     return "Error: not enough flavour in stock"
  96.                 }
  97.                 else {
  98.                     robotIngridients['protein'] -= 10 * quantity;
  99.                     robotIngridients['carbohydrate'] -= 10 * quantity;
  100.                     robotIngridients['fat'] -= 10 * quantity;
  101.                     robotIngridients['flavour'] -= 10 * quantity;
  102.                     return "Success"
  103.                 }
  104.         }
  105.     }
  106.  
  107.     function report() {
  108.         return `protein=${robotIngridients['protein']} carbohydrate=${robotIngridients['carbohydrate']} fat=${robotIngridients['fat']} flavour=${robotIngridients['flavour']}`
  109.  
  110.     }
  111.  
  112.     return function (action) {
  113.  
  114.         let tokens = action.split(" ");
  115.         let command = tokens.shift();
  116.         switch (command) {
  117.  
  118.             case 'restock':
  119.                 return restock(tokens);
  120.  
  121.             case 'prepare':
  122.                 return prepare(tokens);
  123.  
  124.             case 'report':
  125.                 return report();
  126.         }
  127.  
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement