Liliana797979

race2 - fundamentals

Jul 30th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.      
  3. function race(input) {
  4.     let participants = input.shift().split(', ');
  5.     let notWords = /[^a-zA-Z]/g;
  6.     let notDigits = /[^0-9]/g;
  7.     let storeList = {};
  8.  
  9.     while (input[0] != 'end of race') {
  10.         let line = input.shift();
  11.         let name = line.replace(notWords, '');
  12.         let number = line.replace(notDigits, '')
  13.             .split('')
  14.             .map(Number)
  15.             .reduce((a, b) => a + b);
  16.      
  17.         if (participants.includes(name)) {
  18.             if (storeList[name] == undefined) {
  19.                 storeList[name] = number;
  20.             } else {
  21.                 storeList[name] += number;
  22.             }
  23.         }
  24.     }
  25.    
  26.     let sorted = Object.entries(storeList).sort((a, b) => b[1] - a[1]);
  27.  
  28.     for (let i = 0; i < 3; i++){
  29.         switch (i) {
  30.             case 0: console.log(`${i + 1}st place: ${sorted[i][0]}`); break;
  31.             case 1: console.log(`${i + 1}nd place: ${sorted[i][0]}`); break;
  32.             case 2: console.log(`${i + 1}rd place: ${sorted[i][0]}`); break;
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment