Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVASCRIPT : Need help to writing a basic simulation of how a BlackJack dealer d

ID: 3915498 • Letter: J

Question

JAVASCRIPT : Need help to writing a basic simulation of how a BlackJack dealer draws cards

Write a basic simulation of how a BlackJack dealer draws cards. Note: there is no user interaction here (no prompts or alerts, unless you're doing the Challenge portion), just output on the web page.

Assume there are multiple decks shuffled and in play, so don't have to worry about how many 8's could be drawn, for example. We also don't worry about suits or numbering face cards distinctly.

The dealer draws a card (from the shuffled decks, so it's random), always in the range of 1 to 10 (we'll say Aces are always low and face cards are worth 10). Use the random-number generation code we discussed in class.

The dealer keeps drawing cards (aka "hitting") until the cards total of 17 or greater.

After all the cards have been drawn, give a result that says that either the dealer went bust (totaled more than 21), or had a result less than that. Report the total and card count, too.

Here are example output from a "bust" run and a successful run:

"Bust" run:

Successful run:

Challenge Version

In addition to the instructions specified above:

Model a single player in addition to the dealer.

Assume, for now, that the player receives all cards first, then the dealer gets all cards.

Deal the player a starting card, then ask if they want another card.

Keep letting the player request cards until they wish to stop, hit 21, or go bust (with 22+).

You don't need to deal with who won, as the scenarios are a bit complex (i.e., bust vs. high score).

Below is an example of what the output might look like.

BlackJack

Player

Player drew a 8, now has 8
Player drew a 3, now has 11
Player drew a 2, now has 13
Player drew a 5, now has 18

Result: player ended with 4 cards totaling 18

Dealer

Dealer drew a 10, now has 10
Dealer drew a 3, now has 13
Dealer drew a 2, now has 15
Dealer drew a 1, now has 16
Dealer drew a 2, now has 18

Result: dealer ended with 5 cards totaling 18

Explanation / Answer

Main Question

var total = 0;

var rand = 0;

while(total<=21)

{

rand = parseInt(Math.random()*(10)+1);

total+=rand;

console.log('Dealer drew a ' + rand + ', now has ' + total);

}

Challenge Version

var total = 0;

var rand = 0;

var cardNum = 0;

var addMore = confirm("Request new card?");

while(total<=17 && addMore)

{

rand = parseInt(Math.random()*(10)+1);

total+=rand;

console.log('Player drew a ' + rand + ', now has ' + total);

cardNum+=1;

if (total<=17)

{

addMore = confirm("Request new card?");

}

}

if (total>21)

{

console.log('Result : Player went bust with ' + cardNum + ' cards totalling ' + total);

}

else

{

console.log('Result : Player ended with ' + cardNum + ' cards totalling ' + total);

}

total = 0;

rand = 0;

cardNum = 0;

while(total<=17)

{

rand = parseInt(Math.random()*(10)+1);

total+=rand;

console.log('Dealer drew a ' + rand + ', now has ' + total);

cardNum+=1;

}

if (total>21)

{

console.log('Result : Dealer went bust with ' + cardNum + ' cards totalling ' + total);

}

else

{

console.log('Result : Dealer ended with ' + cardNum + ' cards totalling ' + total);

}

Add the script to a simple html file and the results will be printed on the console.

If you feel this answer was satisfactory, please do give a thumbs up. Thanks :)