0
I’m doing a Javascript guessing game where the user has three attempts to guess a random number between 1 and 10, only during the test, whenever I miss the first attempt, I increment the trial variable, but the code finishes running. What am I missing ?
Follows the code
var chute = document.getElementById("chute")
var numero
var tentativa
function aleatorio() {
numero = parseInt(1 + Math.random() * 10)
var chute2 = parseInt(chute.value)
tentativa = 0;
if (chute2 != numero || tentativa != 2) {
alert("Você errou! Tente de novo")
document.getElementById("adivinha_form").reset();
} else if (chute2 != numero && tentativa == 2) {
alert("Suas chances acabaram! O número correto é " + numero)
} else if (chute2 == numero) {
alert("Parabéns! Você acertou")
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Adivinhador</title>
</head>
<body>
<h1>Adivinhador</h1>
<form name="adivinha" id="adivinha_form" action="#">
<p>
Seu chute: <br>
<input type="text" id="chute" name="chute">
</p>
<button onclick="aleatorio()" reset="true">Arriscar</button>
</form>
<script type="text/javascript" src="adivinhacao.js"></script>
</body>
</html>