I am currently in the process of developing a blackjack game using javascript. As of now, I have successfully implemented the dealer's and player's cards. However, when the player decides to draw another card, an issue arises. It seems that the document.write function is lagging behind in writing the required text on the webpage.
while (totalP < 21)
{
r = confirm("Hit?");
if (r==true)
{
document.write("<center><br>You chose to hit. </center>");
p[counter] = deck[Math.floor(Math.random() * deck.length)];
document.write("<center><br>" + x + "'s next card: " + p[counter] + "</center>");
totalP = total(p, totalP);
if (totalP > 21)
{
for (var i=0; i<p.length; i++)
{
value = p[i].substring(0,p[i].length-1);
if (value == "A")
{
totalP = totalP - 10;
break;
}
else if (i == p.length - 1)
{
document.write("<center><br>You busted. Total = " + totalP + " > 21. Dealer Wins!</center>");
break;
}
}
}
}
Although the text "You chose to hit." does appear, the subsequent card information fails to display on the webpage after confirming the action. The confirm dialog box showing "You chose to hit." reappears until the player's hand exceeds 21.
If anyone can provide assistance with this issue, it would be greatly appreciated. Despite my efforts in researching online and troubleshooting independently, I suspect that the unreliability and slowness of the document.write function might be causing the problem compared to other sections of JavaScript code.