0
I am making a guessing game with a number of attempts equal to 3. Whenever the user misses the kick, the number of attempts must fall, but this is not happening. Does anyone know what I’m missing ??
HTML
<!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="valida()" reset="true">Arriscar</button>
<p>Tentativas: <span id="tentativa">3</span> </p>
</form>
<script type="text/javascript" src="adivinhacao.js"></script> <!-- Para converter valor de span no JS, chamar o innerText -->
</body>
</html>
Javascript
var chute = document.getElementById("chute")
var tentativa = document.getElementById("tentativa")
function valida() {
var chute2 = parseInt(chute.value)
var tentativa2 = parseInt(tentativa.innerText)
var numero = parseInt(1 + Math.random() * 10)
if(chute2 >= 0 && chute2 <= 10) {
aleatorio(numero, chute2, tentativa2)
}
else {
alert("Favor digitar um numero de 0 a 10")
}
}
function aleatorio(numero, chute2, tentativa2) {
if (chute2 != numero) {
alert("Você errou! Tente de novo")
document.getElementById("adivinha_form").reset();
}
else if (chute2 != numero && tentativa2 == 2) {
alert("Suas chances acabaram! O número correto é " + numero)
}
else if (chute2 == numero) {
alert("Parabéns! Você acertou")
}
}
You are not updating
tentativa
– andrepaulo
I’ve already tried to do this, but whenever I call javascript, the attempt goes back to the original value
– bcastro95
document.getElementById("adivinha_form").reset();
This is making you reset the number of attempts to 3 ... You have to put this only when the guy gets it right. or end his attempts.– andrepaulo
keeps giving error
– bcastro95