Advertisement
Guest User

Untitled

a guest
Oct 11th, 2019
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  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. //console.log(sortedkeyMaterails);
  47. sortedkeyMaterails.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
  48. // for (let [material, quantity] of sortedkeyMaterails) {
  49. // sortedkeyMaterails.sort(sortDescending);
  50. // if(quantity === sortedkeyMaterails[1][1] ){
  51. // sortedkeyMaterails.sort(sortAlphabetically);
  52. // }
  53. // }
  54.  
  55. let sortedMaterails = Object.entries(materials);
  56. sortedMaterails.sort(sortAlphabetically);
  57. //for (let [material, quantity] of sortedMaterails) {
  58. // sortedMaterails.sort(sortAlphabetically);
  59. //}
  60.  
  61. function sortDescending(firstMaterial, secondMaterial){
  62. let firstQuantity = firstMaterial[1];
  63. let secondQuantity = secondMaterial[1];
  64. return secondQuantity - firstQuantity;
  65. }
  66.  
  67. function sortAlphabetically(firstMaterial, secondMaterial){
  68. let firstMaterialName = firstMaterial[0];
  69. let secondMaterialName = secondMaterial[0];
  70. return firstMaterialName.localeCompare(secondMaterialName);
  71. }
  72. for(let [material, quantity] of sortedkeyMaterails){
  73. console.log(`${material}: ${quantity}`);
  74. }
  75. for(let [material, quantity] of sortedMaterails){
  76. console.log(`${material}: ${quantity}`);
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement