Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
Tournament Winner algorithm javascript using Map?

Hello Tournament Winner is an algorithm challenge where given an array of pairs representing the teams that have competed against each other and an array containing the results of each competition, write a function that returns the winner of the tournament.

Results[i] denote the winner of competitions[i], where 1 in the results array mean the home team won and 0 means the away team won.

This is my code and pseudo-code so far:

function tournamentWinner(competitions, results) {


let currentWinningTeam = "";

  const scoreTracker = new Map();
  scoreTracker.set(currentWinningTeam, 0);
  // since comeptitions and results have same length, use for loop to go through both of the arrays
  // they are in order of results to comeptitionns
  // use hash map to keep track of eachTeams points
  // final loop to find the team with the higest points
  for (const index in competitions) {
    const result = results[index];
    // 0 0 1
    // console.log(result);

    // ["HTML", "C#"] C# > HTML
    const [homeTeam, awayTeam] = competitions[index];

    // console.log(index);
    // #C, Python, Python
    const teamWhoWon = result === 0 ? awayTeam : homeTeam;

    console.log(teamWhoWon)

    updateScores(teamWhoWon, 3, scoreTracker)

    console.log(scoreTracker)
  }
  return currentWinningTeam;
}

function updateScores(teamWhoWon, points, scoreTracker) {
  if (!scoreTracker.has(teamWhoWon)) {
    scoreTracker.set(teamWhoWon, 0)
  } else {
    scoreTracker.set(teamWhoWon, )
  }
}

console.log(
  tournamentWinner(
    [
      ['HTML', '#C'],
      ['#C', 'Python'],
      ['Python', 'HTML'],
    ],
    [0, 0, 1]
  )
);

I am having trouble of how to incrementally increase the points of the team in the scoreTracker hash map in my helper function updateScores()? I am not too familiar with Map methods and can someone show me how I can incrementally increase the points of a team by points?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could get the actual count with Map#get and if falsy, like undefined use zero instead and add one.

m.set(team, (m.get(team) || 0) + 1)

const
    tournamentWinner = (competitions, results) => {
        return [...results.reduce((m, r, i) => 
                (team => m.set(team, (m.get(team) || 0) + 1))
                (competitions[i][+!r]),
            new Map)];
    };

console.log(tournamentWinner([['HTML', '#C'], ['#C', 'Python'], ['Python', 'HTML']], [0, 0, 1]));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda