bebo231312312321

Untitled

Mar 18th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function race(input) {
  2.     let places = {}
  3.     let winer = { 1: '1st', 2: '2nd', 3: '3rd' }
  4.      input.splice(input.indexOf("end of race"), 1)
  5.     let nameLine = input.shift().split(", ")
  6.     input.forEach(line => {    
  7.         let racer = line.match(/[A-Za-z]+/g).join("")
  8.         let distance = line.match(/\d/g).map(x => Number(x)).reduce((a, b) => a + b,0)
  9. if(nameLine.includes(racer)){
  10.         if (!places.hasOwnProperty(racer)) {places[racer] = [];
  11.             places[racer] = 0;
  12.         }
  13.         places[racer] += distance
  14.     }
  15.     });
  16.     Object.entries(places).sort((a, b) => b[1] - a[1]).forEach((x,i)=>{
  17.             if(i<3)console.log(`${winer[i + 1]} place: ${x[0]}`)      
  18.         })
  19.     }
Add Comment
Please, Sign In to add comment