Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda