Advertisement
EntropyStarRover

03. Cooking Factory

Jun 25th, 2019
163
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bakeFactory(input) {
  2.     input = input.slice(0, input.length - 1);
  3.     let batchesObjArr = []
  4.  
  5.  
  6.     for (let i = 0; i < input.length; i++) {
  7.         let batch = input[i];
  8.         let batchArr = batch.split("#");
  9.         batchArr = batchArr.map(Number);
  10.         let sum = 0;
  11.         batchArr.forEach(element => {
  12.             sum += element;
  13.         });
  14.  
  15.  
  16.         batchesObjArr.push({
  17.             batch: batchArr,
  18.             totalQ: sum,
  19.             average: sum / batchArr.length
  20.         });
  21.  
  22.     }
  23.  
  24.     batchesObjArr = batchesObjArr.sort(function (a, b) {
  25.         return b.totalQ - a.totalQ;
  26.     })
  27.     let bestBatch = batchesObjArr[0].totalQ;
  28.  
  29.  
  30.     batchesObjArr = batchesObjArr.filter((element => element.totalQ == bestBatch));
  31.     batchesObjArr = batchesObjArr.sort(function (a, b) {
  32.         return b.average - a.average;
  33.     })
  34.  
  35.     batchesObjArr=batchesObjArr.sort(function(a,b){
  36.         return a.batch.length-b.batch.length;
  37.     })
  38.  
  39.  
  40.     console.log(`Best Batch quality: ${bestBatch}`);
  41.     console.log(batchesObjArr[0].batch.join(" "));
  42. }
Advertisement
RAW Paste Data Copied
Advertisement