Putting it all together
The Resources folder structure should look something like this:
resources
├── images
│ └── .gitkeep
├── scripts
│ ├── rock_paper_scissors.js
│ └── index.js ⬅️
└── styles
└── styles.css
In this step, we will be working on the file marked with the ⬅️
In this final piece of the assignment, we will need to:
- get a handle of all elements of interest.
- then We will add event listeners to the buttons
- We will call on our RockPaperScissors class methods as needed.
- create constants
const
of element handlers to the following elements- welcomeScreen for
#welcome-screen
- gameScreen for
#game-screen
- startGameButton for
#start-game-button
- userName for
#username
- userSelection for
#user-selection
- goButton for
#go-button
- scoreParagraph for
#score
- gameHistoryParagraph for
#game-history
- welcomeScreen for
Example
const welcomeScreen = document.getElementById(`welcome-screen`);
OR, using the querySelector
const gameScreen = document.querySelector(`#game-screen`);
- programmatically, hide the
gameScreen
using Bootstrap’s classd-none
gameScreen.classList.add("d-none")
- add an Event Listener to the
start-game-button
on the click event. a. get username from the text input and store it a variable to be used in the next step. b. instantiate the game object from theRockPaperScissors
class.game = new RockPaperScissors(username);
- Note: The top of the file already has a declared(but not initialized) variable
game
. I’m assigning the value of the new class object to this variable, instead of declaring it in the eventHandler. Remember variables are scoped in JS, if the variable is declared inside the event handler scope, it will only be accessible from there. c. hide the welcomeScreen and display the gameScreen Instead. - tip: you’ll be adding and removing the
d-none
class.
- Note: The top of the file already has a declared(but not initialized) variable
- Create a function named
function updateScoreTallyUI(){...}
- Modify the
#score
paragraph. It should look something like this<USERNAME>: <USER_SCORE> v CPU: <CPU_SCORE>
- i.e. if
username
is Ben and theuserScore
is 3 and thecpuScore
is 5, it would change the scoreParagraph toBen: 3 v CPU: 5
you can get theusername
, andscore
object from the game object.game.username
,game.score
- i.e. if
- Modify the
- Create a function named
function updateGameHistoryUI(){...}
- it makes use of the class property
gameHistoryLog
. - it clears the current game history paragraph
- and replaces it with the content of the array.
- it makes use of the class property
- add an Event Listener to the
go-button
on the click event.- get the userSelection from the select
- call the
play(userSelection)
of thegame
object. - update the text of
scoreParagraph
using theupdateScoreTallyUI()
function - update the
gameHistoryParagraph
using theupdateGameHistoryUI()
function
Next: Finalize