poli1993_

Legendary Farming JS

Oct 10th, 2019
128
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.   let arr = input.split(' ');
  3.   let materials = {};
  4.   let keyMaterails = {
  5.       'shards': 0,
  6.       'fragments': 0,
  7.       'motes': 0
  8.   };
  9.   let winner = '';
  10.   for(let i = 0 ; i <= arr.length-1; i+=2){
  11.     let quantity = Number(arr[i]);
  12.     let  material = arr[i+1].toLowerCase();
  13.  
  14.     if(material === 'shards' || material === 'fragments' || material === 'motes'){
  15.         keyMaterails[material] += Number(quantity);
  16.         if(keyMaterails[material]>= 250 && material === 'shards'){
  17.             winner = 'Shadowmourne' ;
  18.             keyMaterails[material] -= 250;
  19.             break;
  20.          }
  21.          else if(keyMaterails[material]>= 250 && material === 'fragments'){
  22.              winner = 'Valanyr' ;
  23.              keyMaterails[material] -= 250;
  24.              break;
  25.           }
  26.           else if(keyMaterails[material]>= 250 && material === 'motes'){
  27.              winner = 'Dragonwrath' ;
  28.              keyMaterails[material] -= 250;
  29.              break;
  30.           }
  31.     }  
  32.  
  33.     else{
  34.         if(!materials.hasOwnProperty(material)){
  35.             materials[material] = Number(quantity);
  36.         }
  37.         else{
  38.             materials[material] += Number(quantity);
  39.         }
  40.     }
  41.  
  42.   }
  43.   console.log(`${winner} obtained!`);
  44.  
  45.   let sortedkeyMaterails = Object.entries(keyMaterails);
  46.  
  47.   for (let [material, quantity] of sortedkeyMaterails) {
  48.     sortedkeyMaterails.sort(sortDescending);
  49.     if(quantity === sortedkeyMaterails[1][1] ){
  50.         sortedkeyMaterails.sort(sortAlphabetically);
  51.     }
  52.   }
  53.  
  54.   let sortedMaterails = Object.entries(materials);
  55.  
  56.   for (let [material, quantity] of sortedMaterails) {
  57.     sortedMaterails.sort(sortAlphabetically);
  58.   }
  59.  
  60.   function sortDescending(firstMaterial, secondMaterial){
  61.     let firstQuantity = firstMaterial[1];
  62.     let secondQuantity = secondMaterial[1];
  63.     return secondQuantity - firstQuantity;
  64.    }
  65.  
  66.     function sortAlphabetically(firstMaterial, secondMaterial){
  67.        let firstMaterialName = firstMaterial[0];
  68.        let secondMaterialName = secondMaterial[0];
  69.       return firstMaterialName.localeCompare(secondMaterialName);
  70.     }
  71.     for(let [material, quantity] of sortedkeyMaterails){
  72.         console.log(`${material}: ${quantity}`);
  73.     }
  74.     for(let [material, quantity] of sortedMaterails){
  75.         console.log(`${material}: ${quantity}`);
  76.     }
  77. }
RAW Paste Data