我如何减少和增加Javascript中的变量

时间:2018-09-20 21:33:34

标签: javascript

我如何增加和减少JS中的变量? 每当我减少变量VIDA时,VIDA的减少仅适用于Alert,然后VIDA返回3。 在我的代码中,用户需要选择4个单选选项,只有1个正确,并且只有3次尝试

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>

0 个答案:

没有答案