DDEX
DDEX

Reputation: 455

Nested 'for' loop - array undefined

I am working on a JS where I want to create a simple game that starts by chosing number of players, name of each player and whether a player is a dealer or not. There can be only one dealer for each game:

function player(playerName, playerDealer) {
    this.playerName = playerName;
    this.playerDealer = playerDealer;

}
var playerNumber = prompt('Nr of players?');
var playersArray = [];
for (i = 0; i < playerNumber; i++) {
        var j = i + 1;    

        var dealerAssigned = false; // control variable to check whether dealer has been assigned
        var inputName = prompt('Name of player nr  ' + j);
        var inputDealer = prompt('Is player ' + inputName + ' also a dealer? (yes/no)');
        playersArray[i] = new player(inputName, inputDealer);

        for (k=0;k<playerNumber;k++){ // I want to go through the players array to check if dealer has been assigned
        if (playersArray[k].playerDealer == 'yes') {
                dealerAssigned=true;    
                break;
              };    
          };

      if(dealerAssigned){ //if dealer has been assigned, don't add the current player to the array and continue with the next iteration
         alert("already assigned"); 
         continue;
            };

     };

I need to include a simple test into the loop that would check if the dealer has been appointed. If so, I want the script only to alert 'already assigned' and skip to the next player. But I am constantly getting the following error

TypeError: playersArray[k] is undefined

Can anybody explain why is it undefined?/What am I doing wrong?

Upvotes: 1

Views: 1683

Answers (3)

ryanyuyu
ryanyuyu

Reputation: 6486

`This is your structure stipped-down:

for (i = 0; i < playerNumber; i++) {
   playersArray[i] = new player(inputName, inputDealer);
   for (k=0;k<playerNumber;k++)...{

       //anything with index k > i is undefined, since your outer loop
       //hasn't initialized it yet.
   }
}

It seems that your i-loop is trying to insert elements for the size of the array to be, but your k-loop is trying to also access the entire array instead of just the initialized portions. Limit this to for (k=0; k<i+1 ;k++) so you only check the previously initialized values of you playersArray

Upvotes: 0

Sukima
Sukima

Reputation: 10084

Your for loop inside a for loop is iterating over an array that hasn't been filled yet.

First iteration playersArray[j] = new Player(...) makes the array [Player] or an array of one element! Yet the second loop is looking for an array of many elements. once you look for playersArray[1] but there is only playerArray[0] you get undefined and so undefined.playerDealer causes a TypeError.

Upvotes: 0

bmhkim
bmhkim

Reputation: 784

The bug you're specifically asking about appears to me to be that you're iterating over undefined array values, as the error you're getting suggests.

You're getting the number of players you want in line

var playerNumber = prompt('Nr of players?');

Then, you proceed to have two iterations (one nested in the other), in which the inner loop is trying to access values that haven't yet been assigned since the outer loop hasn't gotten there yet:

for (i = 0; i < playerNumber; i++) {
    playersArray[i] = new player(inputName, inputDealer);
    for (k=0; k < playerNumber; k++) {
        if (playersArray[k].playerDealer == 'yes') {
            ...
        }
    }
}

It appears to me that the logical error here is the nested loop. I recommend just initializing all players in one loop, then verify that all players have an assigned dealer afterward.

I should add that I'm being intentionally myopic here and focusing very narrowly on the question asked and overlooking other issues I see.

Upvotes: 2

Related Questions