concept this game in category javascript

appears as: this game
Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

This is an excerpt from Manning's book Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond.

If you created three locks and presented them to a player as Door #1, Door #2, and Door #3, and gave the player a single key, you could ask the player to guess which door their key goes to. If they guess right, they win the prize behind the door. Hopefully that sounds like a fun game because that’s exactly what you’re going to build. Since the locks in this game are going to be doors, the secret can be what’s behind the door. This may seem like a familiar game, but the twist is that the player may choose any door, but only if their key opens that door do they find out what’s behind it and win the prize. Ready? Let’s see how to build it.

In this pop function, you use Array’s pop to get the last card from the deck. You then default the card to being face up. Most games deal face down, but in this game you’ll almost always want the card face up, so default it that way. You use delete to remove the card from the deck, since it obviously can’t be in a player’s hand and remain in the deck at the same time. Finally you return the card.

There’s only one last step to complete this game. You need to add some event listeners to the hit and stay buttons, as shown in the next listing.

Listing 26.20. src/index.js
document.querySelector('.hit-me').addEventListener('click', hit); document.querySelector('.stay').addEventListener('click', stay);

With that, you’re finished. You should now be able to play a game of Blackjack against your computer. Building this game really helped me realize how much the odds are stacked against you when playing against the house.