# Tournament Winner

<figure><img src="https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-b6f03b1cb31ad059d00bcd9e203341c5f0286286%2FScreenshot%202023-01-20%20at%2015.43.28.png?alt=media" alt=""><figcaption></figcaption></figure>

```javascript
function tournamentWinner(competitions, results) {
  const scores = {};
  
  let max = ['', -Infinity];
  let idx = 0;
  while (idx < results.length) {
    const [home, away] = competitions[idx]; // 1 home, 0 away
    const result = results[idx];

    let team = home;
    if (result === 0)
      team = away;
    
    if (scores[team] === undefined)
      scores[team] = 0;
    scores[team] += 3;
    
    if (scores[team] > max[1])
      max = [team, scores[team]];
    
    idx++;

  }
  
  return max[0];
}

// Do not edit the line below.
exports.tournamentWinner = tournamentWinner;
```
