0

How i can increase and decrease a variable in JS? Every time I decrease variable VIDA the decrease of VIDA only applies on Alert, after that VIDA goes back to 3. In my code,the user needs to select 4 radio options and only 1 is correct and in only 3 attempts

var box;
var pontos = 0;
var vida = 3;

function erro() {
  if (vida === 3 || vida === 2) {
    vida--;
    alert('Errow,vidas restantes:' + vida);
  }
  if (vida == 1)
    alert("Perdeu");
}

function acerto() {
  if (vida == "3") {
    pontos += 50;
  }
  if (vida == "2") {
    pontos += 30;
  }
  if (vida == "1") {
    pontos += 10;
  }
  alert("Acertou,numero de pontos:" + pontos);
}


function valida() {
  var perg = document.getElementById('perg').value;
  alert(perg);
  if (document.getElementById('resp1').checked) {
    box = document.getElementById('resp1').value;
  }
  if (document.getElementById('resp2').checked) {
    box = document.getElementById('resp2').value;
  }
  if (document.getElementById('resp3').checked) {
    box = document.getElementById('resp3').value;
  }
  if (document.getElementById('resp4').checked) {
    box = document.getElementById('resp4').value;
  }
  switch (perg) {
    case "1":
      switch (box) {
        case "1":
          acerto();
          break;
        case "2":
          erro();
          break;
        case "3":
          erro();
          break;
        case "4":
          erro();
          break;
      }
      break;
  }
}
<form name="respostas">
  <input id="vida" value="3">
  <input type="hidden" id="perg" value="1">
  <input type="radio" id="resp1" name="resp" value="1">
  <input type="radio" id="resp2" name="resp" value="2">
  <input type="radio" id="resp3" name="resp" value="3">
  <input type="radio" id="resp4" name="resp" value="4">
  <input type="submit" value="submit" onclick="valida()">
</form>
connexo
  • 53,704
  • 14
  • 91
  • 128

0 Answers0