Todorov_Stanimir

03. Cooking Factory

Jun 26th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cookingFactory(input) {
  2.     let currentItem = input.shift().split('#');
  3.     let output = [];
  4.     let bestBatchQuallity = -1001;
  5.     let avaregaQualityOfBestBatchQuallity = 0
  6.     let minLength = 0;
  7.  
  8.     while (currentItem[0] !== 'Bake It!') {
  9.         currentItem = currentItem.map(x=>parseInt(x));
  10.         let currentBatchQuallity = 0;
  11.         for (let i = 0; i < currentItem.length; i++) {
  12.             currentBatchQuallity += currentItem[i];
  13.         }
  14.         let currentAverageQuality = currentBatchQuallity / (currentItem.length);
  15.         let currentLength = currentItem.length;
  16.  
  17.         if (currentBatchQuallity > bestBatchQuallity) {
  18.             output = currentItem;
  19.             bestBatchQuallity = currentBatchQuallity;
  20.             avaregaQualityOfBestBatchQuallity = currentAverageQuality;
  21.             minLength = currentLength;
  22.         } else if (currentBatchQuallity === bestBatchQuallity) {
  23.             if (currentAverageQuality > avaregaQualityOfBestBatchQuallity) {
  24.                 output = currentItem;
  25.                 bestBatchQuallity = currentBatchQuallity;
  26.                 avaregaQualityOfBestBatchQuallity = currentAverageQuality;
  27.                 minLength = currentLength;
  28.             } else if (currentAverageQuality === avaregaQualityOfBestBatchQuallity) {
  29.                 if (currentLength < minLength) {
  30.                     output = currentItem;
  31.                     bestBatchQuallity = currentBatchQuallity;
  32.                     avaregaQualityOfBestBatchQuallity = currentAverageQuality;
  33.                     minLength = currentLength;
  34.                 }
  35.             }
  36.         }
  37.         currentItem = input.shift().split('#');
  38.     }
  39.     console.log(`Best Batch quality: ${bestBatchQuallity}`);
  40.     console.log(output.join(' '));
  41. }
Advertisement
Add Comment
Please, Sign In to add comment