0

In this i have 3 arrays one with suits and other with cards and third with values, i have to create a 2D array with card and assign a value like ♠A, 14

var deck = [];
          function build() {
            var suits = ["♠ ", "♥", "♣", "♦"];
           var name = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"];
           // now create a 2 dim array with each individual array representing a card.. 
            var value = [2,3,4,5,6,7,8,9,10,11,12,13,14];
           for(var i=0; i<suits.length; i++){
            for(x=0; x<name.length; x++)
            {  deck[x]=[2];
              deck.push(suits[i]+name[x]);
            }
            }
           }
  • What would you like your expected result to look like and what's wrong with what you already have? – fortunee Jan 31 '18 at 17:12
  • i want to show user a card like ♠A and assign a value to this card lets say 14 to count for winning and lossing, i have a crad in the code i created but i want to make decck a 2d array and also assign value to card – Manjinder Grewal Jan 31 '18 at 17:15
  • 1
    Possible duplicate of [How can I create a two dimensional array in JavaScript?](https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) – perror Jan 31 '18 at 17:35

1 Answers1

0

This should work:

function build() {
    var deck = []; 
    var suits = ["♠", "♥", "♣", "♦"];
    var name = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"];
    // now create a 2 dim array with each individual array representing a card.. 
    var value = [2,3,4,5,6,7,8,9,10,11,12,13,14];
    for(var i=0; i<suits.length; i++){
        for(x=0; x<name.length; x++)
            {  
               deck.push(suits[i]+name[x]+ ',' + value[x]);
            }
        }

    return deck;
 }

Now, to get the value of the newly built deck, just use:

var newlyBuiltDeck = build();
raneshu
  • 363
  • 2
  • 16
  • 46
  • 2
    Odd that this is accepted as the answer, considering it doesn't actually build a two dimensional array, but rather a single dimensional array of strings with a comma in them. – Heretic Monkey Jun 22 '18 at 21:43