-Enigmos-

footballTournament.js

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