Domanda

I am new to javascript and I need to make a black jack style game. I need to create a table of replicated buttons in a 6x6 row(36 buttons total). They will display an x unless pressed and then they will generate a random number 1 to 9 and add it to a total score in a global variable. If the buttons clicked put you over 21 then you lose but if you get a score of exactly 21 you will win the game and a pop up message will prompt the user of their win.

I not sure how to make a table and then replicate the buttons. I also would like to know how to change the value of the button from x to a random number.

The game will look something like this.

Black Jack Style Game

I am not asking for the full game to be done for me I just want to know how to replicate buttons in a table, point them all to the same function, and change the value displayed on the button when one is clicked. If you can show me an example of anyone of those concepts I would greatly appreciate it.

È stato utile?

Soluzione

how to make a table and then replicate the buttons

Use loops to repeat the same code several times. You can put the code that sets up a button in a loop to make many buttons.

how to change the value of the button

If you're using an input element for the button, the value property lets you change its label.

to a random number

Math.random gives you a random number. You can build some logic around this to get what you want.

how to ... point them all to the same function

You can use event listeners to run code when the user clicks an element.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top