Podcast
Questions and Answers
The scores
array stores the current scores of both players.
The scores
array stores the current scores of both players.
False
The currentScore
variable stores the total score of the active player.
The currentScore
variable stores the total score of the active player.
False
The dice image is displayed in the diceEl
element.
The dice image is displayed in the diceEl
element.
True
The player--active
class is toggled on both player elements.
The player--active
class is toggled on both player elements.
Signup and view all the answers
O elemento btnNew é usado para armazenar as pontuações atuais dos jogadores.
O elemento btnNew é usado para armazenar as pontuações atuais dos jogadores.
Signup and view all the answers
Quando o jogador rola o número 1, sua pontuação atual é zerada e a vez é passada para o próximo jogador.
Quando o jogador rola o número 1, sua pontuação atual é zerada e a vez é passada para o próximo jogador.
Signup and view all the answers
A variável currentScore armazena a pontuação total do jogador ativo.
A variável currentScore armazena a pontuação total do jogador ativo.
Signup and view all the answers
O elemento diceEl é usado para exibir a imagem do dado.
O elemento diceEl é usado para exibir a imagem do dado.
Signup and view all the answers
A classe player--active é adicionada ao elemento do jogador inativo.
A classe player--active é adicionada ao elemento do jogador inativo.
Signup and view all the answers
Quando o jogo é iniciado, as pontuações iniciais dos jogadores são 10.
Quando o jogo é iniciado, as pontuações iniciais dos jogadores são 10.
Signup and view all the answers
O evento de clique no botão btnRoll adiciona a pontuação do dado à pontuação total do jogador.
O evento de clique no botão btnRoll adiciona a pontuação do dado à pontuação total do jogador.
Signup and view all the answers
A variável activePlayer é usada para armazenar a pontuação total do jogador ativo.
A variável activePlayer é usada para armazenar a pontuação total do jogador ativo.
Signup and view all the answers
Study Notes
Selecting Elements
- The code selects elements from the HTML document using
document.querySelector
anddocument.getElementById
methods. - The selected elements include:
-
player0El
andplayer1El
representing the two players. -
score0El
andscore1El
representing the scores of the two players. -
current0El
andcurrent1El
representing the current scores of the two players. -
diceEl
representing the dice element. -
btnNew
,btnRoll
, andbtnHold
representing the three buttons in the game.
-
Initial Setup
- The scores of both players are initially set to 0.
- The
diceEl
element is initially hidden.
Game State
- The game state is stored in the
scores
array, which holds the scores of both players. - The
currentScore
variable holds the current score of the active player. - The
activePlayer
variable keeps track of the active player (0 or 1).
Rolling Dice Functionality
- The
btnRoll
button is assigned an event listener that listens for a click event. - When the button is clicked, the following actions occur:
- A random dice roll is generated using
Math.trunc
andMath.random
methods. - The
diceEl
element is displayed, and itssrc
attribute is set to the corresponding dice image (e.g.,dice-1.png
). - If the dice roll is not 1, the dice value is added to the
currentScore
variable. - If the dice roll is 1, the
currentScore
is reset to 0, and the active player is switched.
- A random dice roll is generated using
Selecting and Initializing Elements
-
player0El
,player1El
,score0El
,score1El
,current0El
,current1El
, anddiceEl
are selected usingquerySelector
andgetElementById
- Initial values of
score0El
andscore1El
are set to 0 -
diceEl
is initially hidden with the classhidden
Rolling Dice Functionality
- The
btnRoll
button is assigned an event listener for a click event - When clicked, a random dice roll is generated using
Math.trunc(Math.random() * 6) + 1
- The
diceEl
image is updated with the corresponding image (dice-${dice}.png
) - If the dice roll is not 1, the current score is updated by adding the dice roll to the current score
- If the dice roll is 1, the current score is reset, and the active player is switched
Game Logic
-
scores
array keeps track of the overall scores of both players -
currentScore
variable keeps track of the current score -
activePlayer
variable determines which player is currently active (0 or 1) - The active player is switched by toggling the
player--active
class onplayer0El
andplayer1El
elements
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your understanding of JavaScript fundamentals, including selecting elements and variables. Identify the correct code snippets and concepts.