Day 3 Of 30 - Building A Blackjack Game (Part-3)

Leader ●2 ●10 ●29
calendar_today • schedule1 min read

Initially in the Blackjack game, I had hardcoded the card numbers.

In today's practice, I made the New Card button generate a random number for every new card.

Here's how I did it:

  • I created a getRandomNumber() function.
    • Used Math.random() method to generate a random number.
    • Math.random generates a number from the range 0.000000 to 0.9999999 (not inclusive of 1).
    • I needed to generate an integer value for every card in the Deck (13 cards in total).
    • To generate this with Math.random(), I used *Math.random() 13. This would generate values within the range of 0.00000 - 12.99999999 (not inclusive of 13).**
    • To get the integer values of the numbers in the above range, I used *Math.floor(Math.random() 13)**. This gave me integer values from 0 to 12.
    • To get the range (1 - 13) I needed for my deck of cards, I mutiplied the above step by 1. Math.floor(Math.random() 13) 1.
    • The Ace in my deck is worth 11 points. To get this, I used an if condition to return 11 if the card value was 1.
    • For the King, Queen and Jack cards, which are all worth 10 points, I used an if condition to return the value 10, if the numbers were greater than 10 (that is 11 to 13).
    • Finally, I assigned the getRandom function() to every card in the cards array (instead of the hard coded values).

And here's what it looks like.

The random numbers it generates.

I also ensured the New game button can no longer draw a card once the game is ended by using a coditional.


PS: It's my first time learning how the Blackjack game even works.😁

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Day 2 of 30 - Building A Blackjack Game (Part - 2)

Liz - Oct 1

My 30-day log cleanup kept a file that was 30 and a half days old

BashSnippets - Oct 8

Day 1 of 30 - I Built A Blackjack Game

Liz - Sep 30

Everyone says DeepSeek is cheaper, but I got tired of guessing the exact math. So I built a calculat

abarth23 - Apr 27

Day 5 of 30 - Tip Calculator App (Part - 2)

Liz - Oct 8
chevron_left
1.6k Points • 41 Badges
Abuja, Nigeria • lizdesigns.vercel.app
9Posts
20Comments
8Connections
API and devTools documentation writer. Learning frontend web development and exploring Agentic AI.

... Show more

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!