Hola, estoy recibiendo un objeto de error de TypeScript que posiblemente no está definido para Esto es dado una matriz de pares que representan a los equipos que han competido entre sí una matriz que contiene los resultados de cada competencia, escribe una función que devuelve el ganador del torneo. La matriz Comeptitions tiene los elementos en forma de [equipo local, equipo visitante] la segunda matriz da como resultado 1 significa que ganó el equipo local y 0 significa que ganó el equipo visitante
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] ) );Soy nuevo en TypeScript y no estoy seguro de por qué recibo este error. He definido todas mis variables.
La clave es que puede agregar as Type cuando trabaja con valores. Por ejemplo (scoreTracker.get(teamWhoWon) as number) .
Podría ayudar con la definición del tipo que devuelve su Mapa. Por favor considere tales ediciones:
// 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] ) );