将得分添加到是/否游戏中

问题描述 投票:0回答:1

我正在创建一个是/否游戏,并且我有一个变量得分,它获得了必须填充的字段的id,每当用户选择正确的答案时,分数需要更新。 if正在检查元素照片是否包含在提到的数组中,如果是,我必须添加一个点,如果,它不是我必须得到点回到0。

如果变量获得id,我怎么能写点?

每当用户得到正确答案时,我该如何添加新点?

我真的无法得到它。谢谢。

var score = document.getElementById('score');

var foto;
var yesMeetup = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16];
var notMeetup = [17, 18, 19, 20, 21, 22, 23, 24, 25, 26];

var notButton = document.getElementById('no');
var yesButton = document.getElementById('yes');

var decisao = document.getElementById('decisao');

document.querySelector('.btn').addEventListener('click', verify);


function verify() {
  yesMeetup;
  notMeetup;

  if (yesButton) {

    if (yesMeetup.includes(foto)) {

      decisao.textContent = "You got it";
      decisao.style.display = "block";
      decisao.style.animation = "anim 1s";
      decisao.style.animationIterationCount = "1";
      parseInt(score.textContent) += "1";

    } else {

      decisao.textContent = "wrong";
      decisao.style.animation = "anim 1s";
      decisao.style.display = "block";
      decisao.style.animationIterationCount = "1";
      score.textContent = "0";
    }
  }
}

document.querySelector('#no').addEventListener('click', VerifyNo);

function VerifyNo() {
  yesMeetup;
  notMeetup;

  if (notButton) {
    if (notMeetup.includes(foto)) {

      decisao.textContent = "You got it";
      decisao.style.display = "block";
      decisao.style.animation = "anim 1s";
      decisao.style.animationIterationCount = "1";

    } else {
      decisao.textContent = "Wrong";
      decisao.style.display = "block";
      decisao.style.animation = "anim 1s";
      decisao.style.animationIterationCount = "1";

    }
  }
}
javascript html variables points
1个回答
0
投票

您可以尝试解决此问题的方法是,通过在文件顶部声明它来全局保留一个单独的得分变量:qazxsw poi

现在,每当用户得到一个答案时,使用var scoreVal = 0;增加分数。最后,在验证函数结束时(在所有if条件之外),您可以调用scoreVal++;,因为即使值已更新或保持不变,它也可以工作。

这是一个小小的工作示例,最低限度:

score.textContent = scoreVal;
var score = 0;
document.getElementById('btn-right').addEventListener('click', right);
var scoreElem = document.getElementById('score');
scoreElem.textContent = score;

function right() {
  scoreElem.textContent = ++score;
}
© www.soinside.com 2019 - 2024. All rights reserved.