Building a deck of cards
I'm able to display the cards box with one logo of suit (clubs) but rest is not visible.
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 = '♠';
}
else if (cardDeck[i].suit == 'Diamonds')
{
var ascii_char = '♦';
}
else if (cardDeck[i].suit == 'Hearts')
{
var ascii_char = '♥';
}
else
{
var ascii_char = '♣';
}
/*{
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);
}
}
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]
.
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
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.