Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

javascript - 从PHP转换后无法读取未定义Javascript属性的“ 0”(Cannot read property '0' of undefined Javascript error after converting from PHP)

I have a Single Round Robin Tournament script that was originally written in PHP to generate a playing schedule.(我有一个Single Round Robin Tournament脚本,该脚本最初是用PHP编写的,用于生成播放时间表。)

I have ported the code into Typescript, see below:(我已将代码移植到Typescript中,如下所示:) export = class SingleRoundRobinTournament { constructor(private teamsCount: number) { // If teamsCount is an odd number, add one number if (teamsCount % 2 == 1) teamsCount + 1; this.teamsCount = teamsCount; this.constructTournament(); } // Function to create pairings constructTournament() { // Used in formula const halfTeamsCount: number = this.teamsCount / 2; // Create pairings array let pairings: number[][][] = []; // Assign home and away variables let team1: number; let team2: number; // Set rounds initial value let rounds: number = 0; // Cycle through formula to create random team pairings for (let teamsCounter = 1; teamsCounter < this.teamsCount; teamsCounter++) { // Increment counter rounds += 1; console.log(rounds); console.log(this.teamsCount); console.log(teamsCounter); if (teamsCounter % 2 === 0) { pairings[rounds][0][0] = this.teamsCount; pairings[rounds][0][1] = teamsCounter; } else { pairings[rounds][0][0] = teamsCounter; pairings[rounds][0][1] = this.teamsCount; } for ( let matchesCounter = 1; matchesCounter < halfTeamsCount; matchesCounter++ ) { team1 = (teamsCounter + matchesCounter) % (this.teamsCount - 1); if (team1 === 0) { team1 = this.teamsCount - 1; } if (teamsCounter - matchesCounter <= 0) { team2 = this.teamsCount - 1 + (teamsCounter - matchesCounter); } else { team2 = (teamsCounter - matchesCounter) % (this.teamsCount - 1); } if (matchesCounter % 2) { pairings[rounds][matchesCounter][0] = team1; pairings[rounds][matchesCounter][1] = team2; } else { pairings[rounds][matchesCounter][0] = team2; pairings[rounds][matchesCounter][1] = team1; } } } return pairings; } }; However, I get the following error:(但是,出现以下错误:) pairings[rounds][0][0] = teamsCounter; ^ TypeError: Cannot read property '0' of undefined Has anyone got any ideas as to what I'm getting wrong here?(有谁对我在这里犯错有任何想法吗?) Any help would be greatly appreciated.(任何帮助将不胜感激。) Strangely enough I got the same error when I ported the code into python.(奇怪的是,当我将代码移植到python中时,我遇到了相同的错误。) I've posted the original and working PHP script below: Cheers(我在下面发布了原始且有效的PHP脚本:干杯) <?php class SingleRoundRobin { /** * The number of teams entered for the single round robin tournament. * * @var string */ private $teamsCount; /** * The half number of teams entered for the single round robin tournament. * * @var string */ private $halfTeamsCount; /** * Pairings generated before being randomised. * * @var array */ private $pairings = array(); /** * @param array $teamsCount * @return void */ public function __construct($teamsCount) { // If the teams count is odd, an extra number is added. if ($teamsCount % 2 == 1) $teamsCount++; $this->teamsCount = $teamsCount; $this->halfTeamsCount = $teamsCount / 2; } /** * Create a single round robin teams array. * * @return array */ public function singleRoundRobinCreate() { // Half teams count $halfTeamsCount = $this->teamsCount / 2; // Teams Counter $rounds = 0; for ($teamsCounter = 1; $teamsCounter < $this->teamsCount; $teamsCounter++) { // Rounds counter. $rounds++; if ($teamsCounter % 2 == 0) { $pairings[$rounds][0][0] = $this->teamsCount; $pairings[$rounds][0][1] = $teamsCounter; } else { $pairings[$rounds][0][0] = $teamsCounter; $pairings[$rounds][0][1] = $this->teamsCount; } // Generate the other ((n / 2) -1 matches). for ($matchesCounter = 1; $matchesCounter < $this->halfTeamsCount; $matchesCounter++) { $team1 = ($teamsCounter + $matchesCounter) % ($this->teamsCount - 1); if ($team1 == 0) { $team1 = $this->teamsCount - 1; } if ($teamsCounter - $matchesCounter <= 0) { $team2 = $this->teamsCount -1 + ($teamsCounter - $matchesCounter); } else { $team2 = ($teamsCounter - $matchesCounter) % ($this->teamsCount - 1); } if ($matchesCounter % 2) { $pairings[$rounds][$matchesCounter][0] = $team1; $pairings[$rounds][$matchesCounter][1] = $team2; } else { $pairings[$rounds][$matchesCounter][0] = $team2; $pairings[$rounds][$matchesCounter][1] = $team1; } } } return $pairings; } }   ask by Jeoff Morris translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error...(错误...)

pairings[rounds][0][0] = teamsCounter; ^ TypeError: Cannot read property '0' of undefined ... Is telling you that it cannot read the property 0 of undefined.(...告诉您无法读取未定义的属性0 。) Right there where it points at:(就在这里指向:) pairings[rounds][0] Which means that(意思就是) pairings[rounds] Is undefiend (and yes, you are trying to read at 0 from it).(是不设防的(是的,您正尝试从中读取为0 )。) Now... why is pairings[rounds] undefined?(现在...为什么pairings[rounds]未定义?) because you didn't initialize it.(因为您没有初始化它。) You clearly want to use it like an array, so initialize it to one before using:(您显然想像数组一样使用它,因此在使用之前将其初始化为一个:) pairings[rounds] = []; I suggest to do that after you change the value of rounds .(我建议您在更改rounds值后执行此操作。) Ah, by the way, you want to inizialize pairings[rounds][0] = [] too, right?(嗯,顺便说一句,您也想使pairings[rounds][0] = []初始化吗?)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...