Question

In: Computer Science

javascript BlackJack i made a blackjack game, the code is below //this method will return a...

javascript BlackJack

i made a blackjack game, the code is below


//this method will return a shuffled deck

function shuffleDeck() {

//this will store array fo 52 objects

const decks = []

const suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades']

const values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One']

//run a loop till 52 times

for (let i = 0; i < 52; i++) {

//push a random item

decks.push({ suit: suits[Math.floor(Math.random() * suits.length)], value: values[Math.floor(Math.random() * values.length)] })

}

return decks

}

//function that will draw four cards form the deck

function draw(decks) {

return {

player: [decks[0], decks[1]],

computer: [decks[2], decks[3]]

}

}

//first shuffle the deck

const shuffled = shuffleDeck()

console.log((shuffled))

//now draw the card

const drawn = draw(shuffled)

console.log("\n\nDrawn Cards :" ,(drawn))

i want to make a node application that will listen for user input. this is what i have so far for it.

function startGame () {
let input;
input = prompt("Welcome to Blackjack. Do you want to play? (Y)es or (N)o").toUpperCase();
if (input == 'Y') {}
}

i am stuck on how to finish the startGame Function.

Solutions

Expert Solution

HI ,

You can follow a procedure where user gves an input ('Y' ) and then the game starts by shuffling the cards follwed by math.random function .

Your code will shiffle the card every time the game starts.

If the user inputs ('N'), the user exits .

#SAMPLECODESTRUCTURE

#userinput

function startGame () {
let input;
input = prompt("Welcome to Blackjack. Do you want to play? (Y)es or (N)o").toUpperCase();

if(input=='Y')

{

  

//this method will return a shuffled deck

function shuffleDeck() {

//this will store array fo 52 objects

const decks = []

const suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades']

const values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One']

//run a loop till 52 times

for (let i = 0; i < 52; i++) {

//push a random item

decks.push({ suit: suits[Math.floor(Math.random() * suits.length)], value: values[Math.floor(Math.random() * values.length)] })

}

return decks

}

//function that will draw four cards form the deck

function draw(decks) {

return {

player: [decks[0], decks[1]],

computer: [decks[2], decks[3]]

}

}

else

exit ;


Related Solutions

javascript BlackJack i made a blackjack game, the code is below //this method will return a...
javascript BlackJack i made a blackjack game, the code is below //this method will return a shuffled deck function shuffleDeck() { //this will store array fo 52 objects const decks = [] const suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades'] const values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One'] //run a loop till 52 times for (let i = 0; i < 52; i++) { //push a random item decks.push({ suit: suits[Math.floor(Math.random() *...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to write an html file (P5.html) that uses JavaScript program to create a Blackjack game. 1. Blackjack Games Rules: a. The object of the game is to "beat the dealer", which can be done in a number of ways: • Get 21 points on your first two cards (called a blackjack), without a dealer blackjack; • Reach a final score higher than the dealer without...
Making a blackjack game in javascript Create a function called createDeck() that will create an array...
Making a blackjack game in javascript Create a function called createDeck() that will create an array of objects (52). Each object contains two properties: suit and value. The suit property will be either 'Hearts', 'Clubs', 'Diamonds', or 'Spades'. The value property will be either 'Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three' , or 'Two''. Note: We will probably want to store possible suits and values in separate arrays. Create a function called shuffleDeck() that will...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
Please take the code below and add some javascript to it. Please use javascript inline. Please...
Please take the code below and add some javascript to it. Please use javascript inline. Please be sure to specify within the webpage with something like 'Click here' too show what was done and how to activate it. Please post in a format that can be directly copied. Thank you in advance HINT: add some fun widgets to the page index-css.html: <!DOCTYPE html> <html lang="en"> <head> <!-- title for web page --> <title>Index-CSS Page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1">...
Twenty One This game is similar to the famous card game blackjack. We will play a...
Twenty One This game is similar to the famous card game blackjack. We will play a one-player version of the game. The game is played for some number N of rounds (we will use N = 10,000), at the end of which the player wins points. The player accumulates points during the whole game, and the objective is, of course, to end up with the maximum number of points. The objective in each round of the game is to score...
Twenty One This game is similar to the famous card game blackjack. We will play a...
Twenty One This game is similar to the famous card game blackjack. We will play a one-player version of the game. The game is played for some number N of rounds (we will use N = 10,000), at the end of which the player wins points. The player accumulates points during the whole game, and the objective is, of course, to end up with the maximum number of points. The objective in each round of the game is to score...
//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know...
//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know how to check for unique elements Given an array of integers check if it is possible to partition the array into some number of subsequences of length k each, such that: Each element in the array occurs in exactly one subsequence For each subsequence, all numbers are distinct. Elements in the array having the same value must be in different subsequences If it is...
Use Javascript to implement the class below to the code below. Create a class Order which...
Use Javascript to implement the class below to the code below. Create a class Order which contains one array member variable that stores Sandwich objects. Initially, this array is empty. Add a function named add that adds a sandwich to the array. Add a getter function named price which iterates over the array and sums up the price of each  sandwich in the array. class Sandwich { constructor(price) { this.price = price; } toString() { return "Sandwich", this.price; } } class...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player,...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player, one hidden, so that only the player who gets it knows what it is, and one face up, so that everyone can see it. There are four players: one human player (user) and three computer players. The players take turns requesting cards, trying to get as close to 21 as possible, but not going over 21. A player may pass. Once a player has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT