TZinovieva

Football Tournament JS

Nov 28th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function footballTournament(input) {
  2.     let teamName = input[0];
  3.     let matches = Number(input[1]);
  4.     let index = 2;
  5.  
  6.     let countW = 0;
  7.     let countD = 0;
  8.     let countL = 0;
  9.     let points = 0;
  10.     for (let i = 2; i <= matches + 1; i++) {
  11.         let matchResult = input[i];
  12.         index++;
  13.  
  14.         if (matchResult === "W") {
  15.             countW++;
  16.             points += 3;
  17.         } else if (matchResult === "D") {
  18.             countD++;
  19.             points += 1;
  20.         } else {
  21.             countL++;
  22.         }
  23.     }
  24.     if (matches === 0) {
  25.         console.log(`${teamName} hasn't played any games during this season.`);
  26.    }
  27.    if (matches >= 1) {
  28.        console.log(`${teamName} has won ${points} points during this season.`);
  29.        console.log(`Total stats:`);
  30.        console.log(`## W: ${countW}`);
  31.        console.log(`## D: ${countD}`);
  32.        console.log(`## L: ${countL}`);
  33.        console.log(`Win rate: ${(countW * 100 / matches).toFixed(2)}%`);
  34.  
  35.    }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment