This JavaScript code represents a simple guessing game where the player needs to guess a secret number. Let's break it down:
-
Variables Initialization:
listaDeNumerosSorteados
: An empty array to store the drawn numbers.numeroLimite
: Defines the upper limit for the range of numbers the player can guess.numeroSecreto
: Holds the secret number randomly generated.tentativas
: Keeps track of the number of attempts the player has made.
-
Helper Functions:
exibirTextoNaTela(tag, texto)
: Displays text on the webpage by selecting an HTML element with the provided tag and updating its content with the given text.exibirMensagemInicial()
: Displays the initial message to the player, prompting them to choose a number between 1 and 10.limparCampo()
: Clears the input field after each guess.gerarNumeroAleatorio()
: Generates a random secret number within the defined range and ensures it has not been previously drawn.
-
Event Handling Functions:
verificarChute()
: Called when the player submits their guess. It compares the guess with the secret number and provides feedback. If the guess is correct, it displays a success message along with the number of attempts and enables the option to restart the game. If the guess is incorrect, it provides a hint about whether the secret number is higher or lower and increments the number of attempts.reiniciarJogo()
: Resets the game by generating a new secret number, clearing the input field, resetting the number of attempts, and displaying the initial message.
-
Initial Execution:
- Calls
exibirMensagemInicial()
to display the initial message.
- Calls
-
HTML Integration:
- The HTML elements
<h1>
,<p>
, and<input>
are targeted by the JavaScript functions to display messages, take input, and display the game status.
- The HTML elements
Overall, this code creates a basic guessing game where the player needs to guess a secret number within a certain range, providing feedback after each guess until the correct number is guessed.