JavaScript Basics Quiz

AppreciativeDogwood avatar
AppreciativeDogwood
·
·
Download

Start Quiz

Study Flashcards

12 Questions

The scores array stores the current scores of both players.

False

The currentScore variable stores the total score of the active player.

False

The dice image is displayed in the diceEl element.

True

The player--active class is toggled on both player elements.

True

O elemento btnNew é usado para armazenar as pontuações atuais dos jogadores.

False

Quando o jogador rola o número 1, sua pontuação atual é zerada e a vez é passada para o próximo jogador.

True

A variável currentScore armazena a pontuação total do jogador ativo.

True

O elemento diceEl é usado para exibir a imagem do dado.

True

A classe player--active é adicionada ao elemento do jogador inativo.

False

Quando o jogo é iniciado, as pontuações iniciais dos jogadores são 10.

False

O evento de clique no botão btnRoll adiciona a pontuação do dado à pontuação total do jogador.

False

A variável activePlayer é usada para armazenar a pontuação total do jogador ativo.

False

Study Notes

Selecting Elements

  • The code selects elements from the HTML document using document.querySelector and document.getElementById methods.
  • The selected elements include:
    • player0El and player1El representing the two players.
    • score0El and score1El representing the scores of the two players.
    • current0El and current1El representing the current scores of the two players.
    • diceEl representing the dice element.
    • btnNew, btnRoll, and btnHold 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 and Math.random methods.
    • The diceEl element is displayed, and its src 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.

Selecting and Initializing Elements

  • player0El, player1El, score0El, score1El, current0El, current1El, and diceEl are selected using querySelector and getElementById
  • Initial values of score0El and score1El are set to 0
  • diceEl is initially hidden with the class hidden

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 on player0El and player1El elements

Test your understanding of JavaScript fundamentals, including selecting elements and variables. Identify the correct code snippets and concepts.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser