Here is the code I have written:
function p_deal(id) {
var card1_val = Math.floor(Math.random() * deck.length);
var card2_val = Math.floor(Math.random() * deck.length);
var card1 = deck[card1_val];
var card2 = deck[card2_val];
var hand = card1 + ", " + card2;
var res = card1_val + card2_val;
document.getElementById(id).innerHTML = hand;
and
function hit(id) {
if (bucket == 0) {
bucket = " ";
}
var card3_val = Math.floor(Math.random() * deck.length);
var nhand = deck[card3_val];
bucket = bucket + " " + nhand + ", ";
bucket_val = bucket_val + card1_val + card2_val + card3_val;
if (bucket_val >= 22) {
var r = confirm("Bust! By " + nhand);
if (r == true) {
refresh();
}
else {
refresh();
}
}
document.getElementById(id).innerHTML = bucket;
light = light + 1;
if (light == 5) {
alert("Five Card Blackjack! You Win!");
refresh();
}
}
The card_val variables within bucket val are from the p_deal(id) function. In order for the program to work, the card_val values must be the same each time both functions are called but they need to be regenerated each time the function is called (so multiple players can have different hands). However, as local variables I find it difficult to use them in another function. What can i do here?
Looks like you want an object: