Question: Develop a Web page with a JavaScript script that generates and displays a hand of five cards from a pack of 52 cards. First, create

Develop a Web page with a JavaScript script that generates and displays a hand of five cards from a pack of 52 cards.

First, create an array that contains the names and numbers of 13 playing cards:

"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"

Then create a second array containing the four suits:

"Hearts", "Diamonds", "Clubs", "Spades"

Use a for loop that iterates five times (for five cards) through both arrays. In each iteration of the loop, use the Math.random() method to generate two random numbers in order to select a card name or number, and a suit from the arrays, and display the resulting card in the browser window. The random numbers should be used to index into the appropriate array.

The format of the output should be

Card 1: 4 of Diamonds

Card 2: Ace of Clubs

Etc.

Note

The Math.random() method returns a random number between 0 and 1. To generate a random integer in the range from min to max (where min and max are two integers, min < max) use a function as follows

function getRandomInt(min, max) {

min = Math.ceil(min);

max = Math.floor(max);

return Math.floor(Math.random() * (max - min + 1)) + min;

}

var num1 = getRandomInt(0, 12);

var num2 = getRandomInt(0, 3);

where num1 is the random integer in the range from 0 to 12 that you will use to select a card name or number and num2 is the random integer in the range from 0 to 3 that you will use to select a suit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!