Building a deck of cards

rahulghaghada

Dec 9th, 2020 08:19 AM

I'm able to display the cards box with one logo of suit (clubs) but rest is not visible.card suits.PNG

this is the site where it's explaining how to create a deck with rendering as well https://devdojo.com/devdojo/create-a-deck-of-cards-in-javascript

heres my version of code

/* Creating Card object */

function createCard(value, suit) {

this.value = value;
this.suit = suit;

}

/* Creating deck with 52 cards */

function createDeck() {

this.value = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];

this.suit = ['Spades', 'Diamonds', 'Hearts', 'Clubs'];

var cards = [];

for (var s = 0; s < this.suit.length; s++)
{
    for (var v = 0; v < this.value.length; v++)
    {
        cards.push (new createCard (v+1, this.value[v], this.suit[s] ));
    }
}

return cards;

}

var cardDeck = new createDeck(); console.log (cardDeck);

window.onload = function () { for (var i = 0; i < cardDeck.length; i++) { div = document.createElement('div'); div.className = 'card';

    if (cardDeck[i].suit == 'Spades')
    {
        var ascii_char = '&spades;';
    }
    else if (cardDeck[i].suit == 'Diamonds')
    {
        var ascii_char = '&diams;';
    }
    else if (cardDeck[i].suit == 'Hearts')
    {
        var ascii_char = '&hearts;';
    }
   else
    {
        var ascii_char = '&clubs;';
    }

    /*{
        var ascii_char = '&' + cardDeck[i].suit.toLowerCase() + ';'
    }*/
    div.innerHTML = '<span class = "number">' + cardDeck[i].value + '</span><span class = "suit">' + ascii_char + "</span>";
    document.body.appendChild(div);
}    

}

thinkverse

Dec 9th, 2020 08:30 AM

Seems like your createCard() function is missing the name argument.

function createCard(value, name, suit)
{
	this.value = value;
	this.name = name;
	this.suit = suit;
}

When you are creating your card you pass in e.g 6, 5, 'Spades' as the arguments but your function only takes value and suit not the name. You're passing in more arguments than expected, in this case the this.value[v] takes the place of this.suit[s].

rahulghaghada

Dec 9th, 2020 09:23 AM

So the reason I haven't included 'name' argument was because I needed only the 'value' and the 'suit'. What i've done is used 'value' instead 'name' because in the site they haven't used the name argument.

https://devdojo.com/devdojo/create-a-deck-of-cards-in-javascript

rahulghaghada

Dec 9th, 2020 09:25 AM

Edit: so I am able to display the cards with the ascii characters.

The issue was with the

cards.push where I removed 'v+1' as the createCard has only 2 arguments.

I misunderstood the code.