Advertisement
braveheart1989

06. Breakfast Robot

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