ErolKZ

Untitled

Nov 19th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4.  
  5. let listOfPart = input.shift().split(', ');
  6.  
  7. // console.log(listOfPart);
  8.  
  9. let arr = [];
  10.  
  11. let arr2 = [];
  12.  
  13. let i = 0;
  14.  
  15. let obj = {};
  16.  
  17.  
  18. while (input[i] !== 'end of race') {
  19.  
  20. let cur = input[i].split('');
  21.  
  22. cur.forEach(el => /[A-Za-z]|[0-9]/.test(el) ? arr.push(el) : false);
  23.  
  24.  
  25. arr2.push(arr.join(''));
  26.  
  27. arr = [];
  28.  
  29. i++;
  30.  
  31. }
  32.  
  33.  
  34. for (let el of arr2) {
  35.  
  36. let name = el.split('').filter(el2 => /[A-Za-z]/.test(el2));
  37.  
  38. let distance = 0;
  39.  
  40. el.split('').forEach(el3 => /[0-9]/.test(el3) ? distance += Number(el3) : false);
  41.  
  42.  
  43. if (obj.hasOwnProperty(name.join(''))) {
  44.  
  45. obj[name.join('')].push(distance);
  46.  
  47. } else if (listOfPart.includes(name.join(''))) {
  48.  
  49. obj[name.join('')] = [distance];
  50.  
  51. }
  52.  
  53.  
  54. }
  55.  
  56.  
  57. for (let key in obj) {
  58.  
  59. obj[key] = obj[key].reduce((acc, el) => acc + el, 0);
  60.  
  61. }
  62.  
  63.  
  64.  
  65. let arr3 = Object.entries(obj).sort((a, b) => b[1] - a[1]);
  66.  
  67. // console.log(obj);
  68.  
  69.  
  70. console.log(`1st place: ${arr3[0][0]}
  71. 2nd place: ${arr3[1][0]}
  72. 3rd place: ${arr3[2][0]}`);
  73.  
  74.  
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment