简体   繁体   English

JavaScript测验未验证答案

[英]Javascript Quiz Not Validating Answers

I am working on a Javascript multiplication quiz. 我正在研究Javascript乘法测验。 Here is my relevant HTML: 这是我相关的HTML:

 // Start Button divs to hide and show // $('#zero').click(function() { $('#questions').show(); $('#qdiv').show(); $('.toDisappear').hide(); $('#correctAnswers').show(); $('#incorrectAnswers').show(); $('#nextBtnOne').remove(); }); // Zeros function // $('#zero').click(function(e) { document.getElementById('questions').innerHTML = '0 x 0'; $('#nextBtn').show(); $('#nextBtnOne').remove(); }); // Zeros testing information // let input = ['0 x 0', '1 x 0', '2 x 0', '3 x 0', '4 x 0', '5 x 0', '6 x 0', '7 x 0', '8 x 0', '9 x 0', '10 x 0', 'All Questions Asked!']; let inputAns = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; let currentAnswer = inputAns[0]; document.getElementById('questions').innerHTML = input[0]; document.getElementById("nextBtn").addEventListener("click", function() { let answers = document.getElementById('answers').value; if (parseInt(answers) === currentAnswer) { document.getElementById("correctAnswers").innerHTML += "✅"; inputAns.shift(); if (!inputAns.length) { console.log("All answers provided!"); } } else { document.getElementById("incorrectAnswers").innerHTML += "❌"; } currentAnswer = inputAns[0]; }); let currentQuestion = input[1]; document.getElementById("nextBtn").addEventListener("click", function() { document.getElementById('questions').innerHTML = currentQuestion; input.shift(); currentQuestion = input[1]; }); document.getElementById("nextBtn").addEventListener("click", function() { if (currentQuestion === inputAns[12]) { $('#nextBtn').hide(); } }); // Clears input box after each question // document.getElementById('nextBtn').addEventListener("click", function() { document.getElementById('answers').value = ''; }); // Makes enter button work // document.getElementById('answers').addEventListener("keyup", function(event) { event.preventDefault(); if (event.keyCode === 13) { document.getElementById('nextBtn').click(); } }); 
 <div class="toDisappear"> <div class="main-div"> <h1>Multiplication Quiz</h1> <h2>What is your number?</h2> </div> <div class="instructions" id="instructions"> <p>You will be given three seconds to answer each fact. If you do not answer it, it will disappear and you will miss it. Are you ready?</p> <button type="submit" id="zero" value="zero">0's</button> <button type="submit" id="one" value="one">1's</button> </div> </div> <div class="qdiv" id="qdiv"> <div class="questions" id="questions"></div> <p><input type="text" name="answers" class="answers" id="answers"></p> <button type="submit" id="nextBtn" class="nextBtn">NEXT!</button> <button type="submit" id="nextBtnOne" class="nextBtnOne">NEXT!</button> </div> <div class="correctAnswers" id="correctAnswers"></div> <div class="incorrectAnswers" id="incorrectAnswers"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

It's a lot, but it works for the zero's. 很多,但它适用于零。 It validates each answer and provides a green check when the answer is correct, a red x when it's incorrect. 它会验证每个答案并在答案正确时提供绿色复选标记,在答案不正确时提供红色复选标记。 However, when I add more numbers, it doesn't work. 但是,当我添加更多数字时,它将不起作用。 Here is the code for the ones (next number up): 以下是代码的代码(下一个数字):

 // Ones function // $('#one').click(function(e) { document.getElementById('questions').innerHTML = '0 x 1'; $('#nextBtnOne').show(); }); // Ones testing information // let inputOne = ['0 x 1', '1 x 1', '2 x 1', '3 x 1', '4 x 1', '5 x 1', '6 x 1', '7 x 1', '8 x 1', '9 x 1', '10 x 1', 'All Questions Asked!']; let inputAnsOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let currentAnswerOne = inputAnsOne[0]; document.getElementById('questions').innerHTML = inputOne[0]; document.getElementById("nextBtnOne").addEventListener("click", function() { let answersOne = document.getElementById('answers').value; if (parseInt(answersOne) === currentAnswerOne) { document.getElementById("correctAnswers").innerHTML += "&#9989;"; inputAnsOne.shift(); if (!inputAnsOne.length) { console.log("All answers provided!"); } } else { document.getElementById("incorrectAnswers").innerHTML += "&#10060;"; } currentAnswerOne = inputAnsOne[0]; }); let currentQuestionOne = inputOne[1]; document.getElementById("nextBtnOne").addEventListener("click", function() { document.getElementById('questions').innerHTML = currentQuestionOne; inputOne.shift(); currentQuestionOne = inputOne[1]; }); document.getElementById("nextBtnOne").addEventListener("click", function() { if (currentQuestionOne === inputAnsOne[12]) { $('#nextBtnOne').hide(); } }); // Clears input box after each question // document.getElementById('nextBtnOne').addEventListener("click", function() { document.getElementById('answers').value = ''; }); // Makes enter button work // document.getElementById('answers').addEventListener("keyup", function(event) { event.preventDefault(); if (event.keyCode === 13) { document.getElementById('nextBtnOne').click(); } }); 

This code works if you get all the answers right, but if you miss one, it counts all the ones after it wrong, no matter the input. 如果您正确地获得了所有答案,那么此代码将起作用,但是如果您错过了一个答案,则无论输入内容如何,​​它都会计算出所有错误之后的答案。 Here is the Codepen: link here 这是Codepen: 此处链接

I've tried everything - what I want to happen is that when you click the ones button, the functions work to make the ones questions appear and validate. 我已经尝试了所有内容-我想发生的事情是,当您单击“一个”按钮时,该功能可以使这些问题出现并验证。 I've tried to loop the functions but can't rename the arrays based on user input (from what I understand) and so that didn't work. 我试图循环这些函数,但无法根据用户输入(据我所知)重命名数组,因此无法正常工作。 I tried a switch and that didn't work. 我尝试了一个开关,但是没有用。 Am I missing something or should I be going about this in a totally different way? 我是否缺少某些东西?还是应该以一种完全不同的方式来解决这个问题? Thanks for any help! 谢谢你的帮助!

Calling addEventListener with a new listener doesn't remove the old listener. 使用新的侦听器调用addEventListener不会删除旧的侦听器。 If you call 如果你打电话

document.getElementById("nextBtnOne").addEventListener("click", function() ...);

multiple times, when you click on the button all of the listeners run, and each of them is looking for a different answer. 多次,当您单击按钮时, 所有侦听器都会运行,并且每个侦听器都在寻找不同的答案。

Rather than adding multiple listeners, you should use a single listener that checks a global variable to see if the answer is correct for the current question. 而不是添加多个侦听器,您应该使用一个侦听器来检查全局变量以查看当前问题的答案是否正确。

 // Ones testing information // let inputOne = ['0 x 1', '1 x 1', '2 x 1', '3 x 1', '4 x 1', '5 x 1', '6 x 1', '7 x 1', '8 x 1', '9 x 1', '10 x 1', 'All Questions Asked!']; let inputAnsOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let currentIndex = 0; // Ones function // $('#one').click(function(e) { $('#questions').text(inputOne[currentIndex]); $('#nextBtnOne').show(); $('#nextBtn').hide(); }); $("#nextBtnOne").click(function() { if (parseInt($("#answers").val()) == inputAnsOne[currentIndex]) { document.getElementById("correctAnswers").innerHTML += "&#9989;"; } else { document.getElementById("incorrectAnswers").innerHTML += "&#10060;"; } currentIndex++; $("#answers").val(""); $('#questions').text(inputOne[currentIndex]); if (currentIndex >= inputAnsOne.length) { console.log("All answers provided!"); $("#nextBtnOne").hide(); } }); // Makes enter button work // document.getElementById('answers').addEventListener("keyup", function(event) { event.preventDefault(); if (event.keyCode === 13) { $('#nextBtnOne').click(); } }); 
 <div class="toDisappear"> <div class="main-div"> <h1>Multiplication Quiz</h1> <h2>What is your number?</h2> </div> <div class="instructions" id="instructions"> <p>You will be given three seconds to answer each fact. If you do not answer it, it will disappear and you will miss it. Are you ready?</p> <button type="submit" id="zero" value="zero">0's</button> <button type="submit" id="one" value="one">1's</button> </div> </div> <div class="qdiv" id="qdiv"> <div class="questions" id="questions"></div> <p><input type="text" name="answers" class="answers" id="answers"></p> <button type="submit" id="nextBtn" class="nextBtn">NEXT!</button> <button type="submit" id="nextBtnOne" class="nextBtnOne">NEXT!</button> </div> <div class="correctAnswers" id="correctAnswers"></div> <div class="incorrectAnswers" id="incorrectAnswers"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

// Zeros function //
$('#zero').click(function(e) {
  document.getElementById('questions').innerHTML = '0 x 0';
    $('#nextBtn').show();
    $('#nextBtnOne').remove();
});

     // Zeros testing information //
  let input = ['0 x 0', '1 x 0', '2 x 0', '3 x 0', '4 x 0', '5 x 0', '6 x 0', '7 x 0', '8 x 0', '9 x 0', '10 x 0', 'All Questions Asked!'];
  let inputAns = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  let currentAnswer = inputAns[0];
  document.getElementById('questions').innerHTML = input[0];

document.getElementById("nextBtn").addEventListener("click", function() {
  let answers = document.getElementById('answers').value;
  if (parseInt(answers) === currentAnswer) {
    document.getElementById("correctAnswers").innerHTML += "&#9989;";
    inputAns.shift();
    if (!inputAns.length) {
      console.log("All answers provided!");
    }
  } else {
    document.getElementById("incorrectAnswers").innerHTML += "&#10060;";
  }
  currentAnswer = inputAns[0];
});

  let currentQuestion = input[1];
  document.getElementById("nextBtn").addEventListener("click", function() {
  document.getElementById('questions').innerHTML = currentQuestion;
  input.shift();
currentQuestion = input[1];
  });

document.getElementById("nextBtn").addEventListener("click", function() {
 if (currentQuestion === inputAns[12]) {
     $('#nextBtn').hide();
 }
});

// Clears input box after each question //
document.getElementById('nextBtn').addEventListener("click", function() {
    document.getElementById('answers').value = '';
});

// Makes enter button work //
document.getElementById('answers').addEventListener("keyup", function(event) {
    event.preventDefault();
    if (event.keyCode === 13) {
        document.getElementById('nextBtn').click();
    }
});

// Ones function //
$('#one').click(function(e) {
  document.getElementById('questions').innerHTML = '0 x 1';
    $('#nextBtnOne').show();
    $('#nextBtn').hide();
});

// Ones testing information //
let inputOne = ['0 x 1', '1 x 1', '2 x 1', '3 x 1', '4 x 1', '5 x 1', '6 x 1', '7 x 1', '8 x 1', '9 x 1', '10 x 1', 'All Questions Asked!'];
let inputAnsOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let currentAnswerOne = inputAnsOne[0];
console.log("currentAnswerOne is" + currentAnswerOne);
document.getElementById('questions').innerHTML = inputOne[0];

document.getElementById("nextBtnOne").addEventListener("click", function() {
let answersOne = document.getElementById('answers').value;
if (parseInt(answersOne) === currentAnswerOne) {
document.getElementById("correctAnswers").innerHTML += "&#9989;";
inputAnsOne.shift();
if (!inputAnsOne.length) {
 console.log("All answers provided!");
}
} else {
document.getElementById("incorrectAnswers").innerHTML += "&#10060;";
}
currentAnswerOne = inputAnsOne[0];
});

let currentQuestionOne = inputOne[1];
document.getElementById("nextBtnOne").addEventListener("click", function() {
document.getElementById('questions').innerHTML = currentQuestionOne;
inputOne.shift();
currentQuestionOne = inputOne[1];
});

document.getElementById("nextBtnOne").addEventListener("click", function() {
if (currentQuestionOne === inputAnsOne[12]) {
$('#nextBtnOne').hide();
}
});

// Clears input box after each question //
document.getElementById('nextBtnOne').addEventListener("click", function() {
document.getElementById('answers').value = '';
});

// Makes enter button work //
document.getElementById('answers').addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
   document.getElementById('nextBtnOne').click();
}
});



// Information relevant to all numbers //

// Start Button divs to hide and show //
$('#zero').click(function() {
  $('#questions').show();
  $('#qdiv').show();
  $('.toDisappear').hide();
  $('#correctAnswers').show();
  $('#incorrectAnswers').show();
  $('#nextBtnOne').hide();
});

$('#one').click(function() {
  $('#questions').show();
  $('#qdiv').show();
  $('.toDisappear').hide();
  $('#correctAnswers').show();
  $('#incorrectAnswers').show();
  $('#nextBtnOne').show();
  });

Just added this block of code at the bottom and changed $('#nextBtnOne').remove(); 只需在底部添加此代码块,然后更改$('#nextBtnOne')。remove(); to $('#nextBtnOne').hide(); 到$('#nextBtnOne')。hide();

$('#one').click(function() {
  $('#questions').show();
  $('#qdiv').show();
  $('.toDisappear').hide();
  $('#correctAnswers').show();
  $('#incorrectAnswers').show();
  $('#nextBtnOne').show();
  });

I think everything worked from there. 我认为一切都从那里开始。

Let me know if there's still problems. 让我知道是否还有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM