我正在创建一个是/否游戏,并且我有一个可变的分数,该分数获取必须填写的字段的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";
}
}
}
答案 0 :(得分:0)
您可以尝试解决此问题的方法是,通过在文件顶部声明var scoreVal = 0;
现在,每当用户正确回答一个答案时,就使用scoreVal++;
来增加分数。最后,在您的verify函数结束时(除了所有if条件之外),您可以调用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;
}
<h4 id="score"></h4>
<button id="btn-right">Right</button>
<button id="btn-wrong">Wrong</button>