简体   繁体   中英

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. I have ported the code into Typescript, see below:

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. I've posted the original and working PHP script below: Cheers

<?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;
    }
}

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. 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).

Now... why is pairings[rounds] undefined? 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 .

Ah, by the way, you want to inizialize pairings[rounds][0] = [] too, right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM