Advertisement
nikolayneykov92

Untitled

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