【问题标题】:JavaScript Quiz Validating with onClick Function?使用 onClick 函数验证 JavaScript 测验?
【发布时间】:2015-09-23 17:20:55
【问题描述】:

我正在尝试创建一个 JavaScript 测验。 该函数将检查用户的输入值。 如果正确;它会改变问题。

具体代码见JSFiddle

可能有许多更有效和更传统的方法来实现我想要做的事情。当前的问题是函数每次运行时都会从顶部运行(显然)

function checkAnswer() {

  var question = document.getElementById("ques").innerHTML;

  var userAnswer = document.getElementById("answer").value;

  if (userAnswer === "New York City") {

    alert("correct!");
    question = "What is the best college football team?";

    if (userAnswer === "Alabama") {
      alert("Correct!");
      question = "Next question will go here and so on..."

    }
  }
}

【问题讨论】:

  • 酷!有什么问题?
  • 页面上是否有多个问题需要验证?

标签: javascript function if-statement switch-statement


【解决方案1】:

我绝不会建议以这种方式做事,但这里是让你的 jsfiddle 工作的方法:

function check() {
    var question = document.getElementById('question').innerHTML
    var userAnswer = document.getElementById("answer").value;

    //Makes answer lowercase
    userAnswer = userAnswer.toLowerCase();

    //question one
    if (question === "Write One, Two, Three..") {
        if (userAnswer === "one two three") {
            alert('correct');
        }
        else {
            alert('Sorry Wrong!');
        }

        //question two  
        document.getElementById('question').innerHTML = "Write 4, 5, 6";
    }
    else {
        if (userAnswer === "4 5 6") {
            alert("correct!");
        }
        else {
            alert('Sorry Wrong!');
        }   
    }
}

做你想做的一个简单的方法是将你的问题放在一个数组中:

var QandA = [
    ["question1...", "answer1...."],
    ["question2...", "answer2...."],
    ["question3...", "answer3...."],
    ["question4...", "answer4...."]
];

function check()
{
    // No more questions?
    if (0 === QandA.length) return;

    // Check answer
    var userAnswer = document.getElementById("answer").value.toLowerCase();
    if (userAnswer === QandA[0][1]) {
        alert("Correct");
    }
    else {
        alert("Incorrect");
    }
    // Delete that question
    QandA.shift();
    // And move to next
    if (0 != QandA.length) {
        document.getElementById('question').innerHTML = QandA[0][0];
    }
}

【讨论】:

  • 谢谢,这对我有用。但是,现在我很好奇。你会如何处理这个问题?而且,为什么对于第一个问题你抓住“QandA [0] [1]”但对于第二个问题你两次调用第一个项目(QandA [0] [0])
  • 您最大的问题是用户可以查看网页的来源并看到所有答案。我可能会使用 Ajax 从服务器获取问题和答案。
  • QandA 是一个二维数组。所以,QandA[n][0] 是任何给定n(索引)的问题,QandA[n][1] 是任何给定n 的答案。对象数组可能会更好。
  • 实际上是约翰尼 - 我刚刚想通了。如果你真的必须像我一样在一个函数中编写代码,这实际上是一个非常天才的解决方案。
【解决方案2】:

如果您有很多问题需要验证,我会采用以下方法。它允许您提出任意数量的问题,而无需重复代码。

首先,将您的问题存储在一个数组中:

var arr = ["one two three", "4 5 6"];

将计数器设置为零和总计(以衡量用户性能):

var count = 0;
var total = 0;

缓存元素:

var questionEl = document.getElementById('question');
var userAnswerEl = document.getElementById("answer");

分离出将问题写入新函数的代码。它根据计数器写出问题:

function writeQuestion() {
    if (count < arr.length) {
      questionEl.innerHTML = "Write " + arr[count];
    } else {
      alert('No more questions. You have scored ' + total);
    }
}

function check() {
    userAnswer = userAnswerEl.value.toLowerCase();   
    if (userAnswer === arr[count]) {
        alert('correct');
        count++;
        total++;
        writeQuestion();
    } else {
        alert('Sorry Wrong!');
        count++;
        writeQuestion();
    }
}

DEMO

【讨论】:

    【解决方案3】:
    if (userAnswer === "New York City") {
    
      alert("correct!");
      question = "What is the best college football team?";
    
      if (userAnswer === "Alabama") {
        alert("Correct!");
        question = "Next question will go here and so on..."
    
      }
    }
    

    这个块只在userAnswer"New York City" 时运行,但是在里面,你测试userAnswer 是否是"Alabama" - 那永远不会是真的。您可能希望将第二个 if 移到第一个 if 块之外。

    您似乎正在分配给question,但没有使用该值。如果您认为通过为question 分配一个值来更新问题文本,这是行不通的。我认为您正在尝试这样做:

    question = document.getElementById("ques");
    
    // later on...
    question.innerHTML = "this is another question";
    
    // even later...
    question.innerHTML = "and here is a new question";
    

    这将为您更新页面,因为 question 将指向一个 DOM 节点,您可以在其上设置 .innerHTML,但是您编写它的方式,您最初将 question 设置为字符串值,然后是其他字符串值,但无论如何都不使用它们中的任何一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 2017-12-27
      相关资源
      最近更新 更多