-1

I'm trying to do something like drawing cards.

I have an array with 52 elements (deck[]), and I want to remove the first 13 elements and put them into another array, lets say player1[].

Then remove the next 13 elements and put them into player2[]...and so on.

I did this:

var deck = [], player1 = [], player2 = [], player3 = [], player4 = [];

function distributeCards(){
  for(var i = 1; i < 5; i++){               
    for(var j = 0; j < 13; j++){
      player+i.push(deck.shift(j));
    }
  }
}

The array variables are declared outside, because I have to access them in other functions.

It says player is not defined...how should I write this?

razoes
  • 183
  • 11
  • 2
    What you want is a dynamic variable name: See here: https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript – Christian Jun 28 '15 at 02:50
  • @ChristianVarga: The only dynamic variable names are global variables. People usually don't like to pollute the global space that much. –  Jun 28 '15 at 03:06
  • @squint That's not true; you can use `eval()` to generate a dynamic variable name (as described in the link I posted). – Christian Jun 29 '15 at 04:26
  • @ChristianVarga: That isn't truly a dynamic variable name (as in a language feature). That's simply evaluating a program and injecting it into the current scope. Not to mention its undesirable side-effects. –  Jun 29 '15 at 15:52
  • @squint You're just clawing at technicalities. What's your point? Whether it's a language feature or not, it allows you to create a dynamic variable name. Also, I'm curious - what would be the undesirable side-effects if used in the OPs scenario? – Christian Jun 30 '15 at 02:22
  • The problem with your comment is that you suggest he needs something that he doesn't. Now if JavaScript *actually* had dynamic variables (beyond globals), you'd be correct. Because it doesn't, one would have to resort to *mimicking* them with `eval` to take your advice. There are *much* better solutions for JS. Programming is *all* about technicalities. As to the problem with `eval`, it destroys optimizations in modern JS engines. OP's scenario is irrelevant. Give people bad advice and they'll continue to do it in other scenarios. –  Jun 30 '15 at 14:27

1 Answers1

1

You can't make up variable name with that. Instead, you should consider using array to store player's card, so you can dynamically reference each of the player's deck like this:

var deck = [];
var numOfPlayers = 4;
var players = new Array(numOfPlayers);

function distributeCards(){
  for(var i = 0; i < numOfPlayers; i++){ 
    players[i] = [];              
    for(var j = 0; j < 13; j++){
      players[i].push(deck.shift(j));
    }
  }
}
TaoPR
  • 5,932
  • 3
  • 25
  • 35
  • Even though your solution is better, for the sake of correctness I just want to point out that you _can_ do what the OP suggested - see my comment above. – Christian Jun 28 '15 at 02:54
  • Yes, you can do that way. Your link is generically useful. However, it's more suitable to use array for this purpose. – TaoPR Jun 28 '15 at 03:02
  • @thefourtheye Very thoughtfully suggestion. Let me update it. – TaoPR Jun 28 '15 at 03:18
  • @TaoP.R. Thanks for your answer. Can you tell me why `alert(players[0][0].id);` doesn't work? I created my deck[] by doing `var deck = table.children;` because I already have the card elements on the table. It says "Cannot read property 'id' of undefined". – razoes Jun 28 '15 at 04:09
  • @razoes Have you populated the `deck` beforehand? I guess you will run into this situation when your `deck` variable is not filled before calling `distributeCards()`. – TaoPR Jun 28 '15 at 04:14
  • @TaoP.R. As I said, your solution is better. I'm just pointing out that your statement (_"You can't make up variable name with that"_) is not true/correct. You _can_ have dynamic variables in javascript. – Christian Jun 29 '15 at 04:28
  • @TaoP.R. Thanks. This worked out for what I needed it to. Only thing I changed was in `shift(j)` I removed the `j`. It was other member sugestion and does work. – razoes Jun 29 '15 at 18:13