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

305
Visualizações
TypeScript Error TS2532: Object is possibly 'undefined'?

Hello I am getting a TypeScript error object is possibly undefined for This is for given an array of pairs representing the teams that have competed against each other an array containing the results of each competition, write a function that returns the winner of the tournament. Comeptitions array has the elemtns in form of [hometeam, awayteam] the second array results 1 means the home team won and 0 means the away team won

    if (scoreTracker!.get(teamWhoWon) > scoreTracker!.get(currentWinningTeam)) {
  currentWinningTeam = teamWhoWon
}


This is for an algorithm data challenge of this whole function::

    export function tournamentWinner(competitions: string[][], results: number[]) {
  // Write your code here.
  let currentWinningTeam = "";

  const scoreTracker: Map<string, number>= 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
  // TIME COMPLEXITY O(N) = linear 1 loop.
  // if i had 2 loops O(N^2) = quadratic
  // SPACE COMPLEXITY O(K) = memory you created
  for (const index in competitions) {

    const result: number = 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: string = result === 0 ? awayTeam : homeTeam;

    console.log('teamwhowon', teamWhoWon)

    updateScores(teamWhoWon, 3, scoreTracker)
    console.log('scoreTracker', scoreTracker)

    if (scoreTracker!.get(teamWhoWon) > scoreTracker!.get(currentWinningTeam)) {
      currentWinningTeam = teamWhoWon
    }
  }
  return currentWinningTeam;
}

function updateScores(teamWhoWon: string, points: number, scoreTracker: Map<string, number>) {
  if (!scoreTracker?.has(teamWhoWon)) {
    scoreTracker?.set(teamWhoWon, 3)
  } else {
    scoreTracker?.set(teamWhoWon, scoreTracker?.get(teamWhoWon) + points)
  }
}

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

I am new to TypeScript and I'm not sure why I am getting this error. I've defined all my variables.

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

0

The key thing is that you could add as Type when working with values. For example (scoreTracker.get(teamWhoWon) as number).

You could help with definition of type which returns your Map. Please consider such edits:

//  if (scoreTracker!.get(teamWhoWon) > scoreTracker!.get(currentWinningTeam)) {
//   currentWinningTeam = teamWhoWon
// }


// This is for an algorithm data challenge of this whole function::

    export function tournamentWinner(competitions: string[][], results: number[]) {
  // Write your code here.
  let currentWinningTeam = "";

  const scoreTracker: Map<string, number>= 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
  // TIME COMPLEXITY O(N) = linear 1 loop.
  // if i had 2 loops O(N^2) = quadratic
  // SPACE COMPLEXITY O(K) = memory you created
  for (const index in competitions) {

    const result: number = 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: string = result === 0 ? awayTeam : homeTeam;

    console.log('teamwhowon', teamWhoWon)

    updateScores(teamWhoWon, 3, scoreTracker)
    console.log('scoreTracker', scoreTracker)

    if ((scoreTracker.get(teamWhoWon) as number) > (scoreTracker.get(currentWinningTeam) as number)) {
      currentWinningTeam = teamWhoWon
    }
  }
  return currentWinningTeam;
}

function updateScores(teamWhoWon: string, points: number, scoreTracker: Map<string, number>) {
  if (!scoreTracker?.has(teamWhoWon)) {
    scoreTracker?.set(teamWhoWon, 3)
  } else {
    scoreTracker?.set(teamWhoWon, (scoreTracker?.get(teamWhoWon) as number) + points)
  }
}

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