I'm quite a beginner in Javascript and trying to make a blackjack game.
Arrays and objects:
card = {}, //every card is an object having 3 properties: suit, number and points
playerCards = [], //an array containing player cards (objects)
dealerCards = [], //an array containing dealer cards (objects)
I also created manually some cards for testing purposes, like this:
card = {
suit : "spade",
number : "queen",
points : 10
};
playerCards.push(card);
Then i have functions to randomly create new cards and check if it's already in use:
function makeCard() {
"use strict";
var uK;
for (;;) {
createCard(card, suit, number, points); // creates a new card, code below
checkCard(card, playerCards, dealerCards, uC); // checkes if card is already in use and returns uC = 1 if "yes" and uC = 0 if "not" (working)
if (uK === 0) {
break;
}
}
return card;
}
Here's the code of createCard():
function createCard(card, suit, number, points) {
"use strict";
var i = Math.floor(Math.random() * 4);
card.suit = suit[i];
i = Math.floor(Math.random() * 13);
card.number = number[i];
card.points = points[i];
return card;
}
It works, but when it creates a new card it replaces the last one manually added in playerCards/dealerCards array. I can prevent it with creating a pointless sample card and not adding it to array (then this one will be replaced), but it doesn't seem right. So what should i do?
Thanks in advance!
createCardand adds the result to the array?