Build a Coin Toss betting game!
- Tool: Use 3rd-party testing library
- Variables: Declaring and assign
const
variable - Variables: Assign variable from property of global object
- Testing: TDD a pure function
- Functions: Calling a function
- Functions: Using an anonymous function as a function argument
- Functions: Define function parameter
- Testing: Test assertions with
assert.equal
Setup your document title and styled header
New T&C:
- HTML Elements:
<title>
,<h1>
- CSS Styling: Use
body
for global styling - CSS Styling:
font-family
,background
,color
,text-align
,font-size
,font-weight
User clicks the flip button and the app randomly determines heads or tails and shows an image corresponding to the result.
New T&C:
- HTML Elements:
<button>
,<section>
,<img>
- CSS Selectors: id (
#
), tag, and class (.
) - CSS Styling:
visibility
,margin
,padding
,margin: auto
,min-height
- ESModules:
export default
andimport
default - Built-in (Browser): global
document
- DOM Traversal:
document.getElementById('id')
matches HTML attributeid="id"
- Built-in (JavaScript):
Math.random()
returns 0 to <1 - Variables: Declare
let
variable with sensible default - Control Flow:
if
andelse
conditionality - Functions: Call a function with a variable
- Variables: Reassign
let
variable in conditional block - DOM Node: set property
image.src =
- DOM Node: remove class
element.classList.remove('class-to-remove')
- Functions: Call a function with two params
- DOM Events: subscribe to
click
event withaddEventListener
User chooses heads or tails then clicks the flip button (note: now a form submit, not just a button click). The app performs the flip as above, then determines whether the user won or loss and displays an appropriate message.
New T&C:
- HTML Elements: named radio inputs
- HTML Attributes:
required
inputs - App Design: evolve code: button
click
becomes formsubmit
- DOM Events: subscribe to form
subscribe
event withaddEventListener
- DOM Events: use
event
callback argument to prevent default behavior - DOM Node: add class
element.classList.add('class-to-add')
- DOM Node: set text
element.textContent =
The app displays a "wins" and "losses" count (both initially 0
). As each flip bet is determined above, the app increments the appropriate count.
New T&C:
- Variables: Declare module-level
let
variable as state - Variables: Reassign module-level state variable
The app displays the amount of money the user has (pick a default like $1000). The user is prompted for an amount for each bet along with their guess. The app adds or subtracts the bet amount from the total money of money the user has.
New T&C:
- HTML Attribute: numeric type input
- Built-in (Browser): Format number as currency with
.toLocaleString()
The app stops the game and alerts the user if the amount goes negative
New T&C:
- App Design: Two-stage logic checking (toss check, then broke check)
- HTML Attributes: Disable button with
button.disabled = true