PrepareTheDrill
PrepareTheDrill

Reputation: 59

Trying to create a deck of cards using For Loop

I am trying to create a deck of cards using 2 arrays. One array is the suits and the other is the values. My thought was to use a for loop to iterate through an array to create the 52 cards deck. However, I can't seem to get the syntax right.

I tried using forEach method.

var deck = [];
var suits = ["diamonds", "spades", "hearts", "clubs"];
var values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

suits.forEach(function(suits) {
  deck.push(suits);
  deck.push(values);
});

console.log(deck)

Upvotes: 4

Views: 1578

Answers (4)

p.s.w.g
p.s.w.g

Reputation: 149050

You need two loops, one over the suits, and one over the face values, e.g.

var deck = [];
var suits = ["diamonds", "spades", "hearts", "clubs"];
var values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

suits.forEach(function(suit) {
  values.forEach(function(value) {
    deck.push(`${value} of ${suit}`);
  });
});

console.log(deck);

Alternatively, you can use flatMap:

var suits = ["diamonds", "spades", "hearts", "clubs"];
var values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
var deck = suits.flatMap(suit => values.map(value => `${value} of ${suit}`));

console.log(deck);

Note: flatMap is not supported by some older browsers, so you may need polyfill.

And just for demonstration purposes, you could also do this with Ramda's map and xprod:

var suits = ["diamonds", "spades", "hearts", "clubs"];
var values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
var deck = R.map(([suit, value]) => `${value} of ${suit}`, R.xprod(suits, values));

console.log(deck);
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

Upvotes: 4

A Rogue Otaku
A Rogue Otaku

Reputation: 923

I would suggest something like this:

var deck = [];
var suits = ["diamonds", "spades", "hearts", "clubs"];
var values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

suits.forEach(function(suit) {
  values.forEach(function(value) {
    deck.push([suit, value]);
  });
});

console.log(deck)

Upvotes: 0

shadow2020
shadow2020

Reputation: 1351

var deck = new Array();
var suits = ["diamonds", "spades", "hearts", "clubs"];
var values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];


function makeDeck()
{

    for(var i = 0; i < suits.length; i++)
    {
        for(var x = 0; x < values.length; x++)
        {
            var card = {Value: values[x], Suit: suits[i]};
            deck.push(card);
        }
    }

    return deck;
}

makeDeck();

Upvotes: 0

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92567

try

suits.forEach(s=> values.forEach(v=> deck.push(v+' of '+s)));

var deck = [];
var suits = ["diamonds", "spades", "hearts", "clubs"];
var values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

suits.forEach(s=> values.forEach(v=> deck.push(v+' of '+s)));

console.log(deck);

Upvotes: 1

Related Questions