Liliana797979

race - fundamentals

Jul 30th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.    function foo(arr) {
  3.     const participants = arr.shift().split(', ').reduce((a, v) => { a[v] = 0; return a }, {})
  4.     const places = { 1: '1st', 2: '2nd', 3: '3rd' }
  5.     arr = arr.map(x => x.split(/[^a-zA-Z0-9]+/g)).map(x => x.filter(X => X !== '')).map(x => x.join(''))
  6.  
  7.     for (let i = 0; i < arr.length; i++) {
  8.         let name = arr[i].split(/[0-9]+/g).filter(x => x !== '').join('')
  9.         let distance = arr[i]
  10.             .split(/[a-zA-Z]+/g)
  11.             .filter(x => x !== '')
  12.             .join('')
  13.             .split('')
  14.             .map(x => Number(x))
  15.             .reduce((a, v) => a + v, 0)
  16.  
  17.         if (Object.keys(participants).includes(name)) {
  18.             participants[name] += distance
  19.         }
  20.     }
  21.     const finalTable = Object.entries(participants).sort((x, y) => y[1] - x[1])
  22.  
  23.     for (let i = 0; i < 3; i++) {
  24.         console.log(`${places[i + 1]} place: ${finalTable[i][0]}`)
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment