[英]Typescript array semantic error TS2532: Object is possibly 'undefined'
[英]TypeScript Error TS2532: Object is possibly 'undefined'?
您好,我收到 TypeScript 錯誤 object 可能是未定義的,這是因為給定一組代表已相互競爭的團隊的對數組包含每場比賽的結果的數組,編寫一個 ZC1C425268E68385D1AB5074C 錦標賽的獲勝者。 比賽數組有 [hometeam, awayteam] 形式的元素 第二個數組結果 1 表示主隊獲勝,0 表示客隊獲勝
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]
)
);
我是 TypeScript 的新手,我不確定為什么會收到此錯誤。 我已經定義了所有變量。
關鍵是您可以在使用值時添加as Type
。 例如(scoreTracker.get(teamWhoWon) as number)
。
您可以幫助定義返回 Map 的類型。 請考慮這樣的編輯:
// 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] ) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.