Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript选择题测验_Javascript_Html - Fatal编程技术网

JavaScript选择题测验

JavaScript选择题测验,javascript,html,Javascript,Html,我正在创建一个选择题测验。出于某种原因,它没有运行。我已经通过javascript控制台运行了它,它一直告诉我submitAnswer不是一个函数。 以下是javascript代码: var userAnswers = []; var answered = 0; var questions = [question1, question2, question3, question4, question5, question6, question7, question8, question9, q

我正在创建一个选择题测验。出于某种原因,它没有运行。我已经通过javascript控制台运行了它,它一直告诉我submitAnswer不是一个函数。 以下是javascript代码:

var userAnswers = [];
var answered = 0; 
var questions = [question1, question2, question3, question4, question5, question6, question7, question8, question9, question10];
var answers = [q1a, q1b, q1c, q1d, q2a, q2b, q2c, q2d, q3a, q3b, q3c, q3d, q4a, q4b, q4c, q4d, q5a, q5b, q5c, q5d, q6a, q6b, q6c, q6d, q7a, q7b, q7c, q7d];
function resetQuiz(showConfirm) {
  if(showConfirm)
    if(!confirm("Are you sure you want to reset the quiz and start from the beginning?"))
      return false;
  document.location = document.location;
}

var submitAnswer = function(questionId, obj, classId, labelId) {
  userAnswers[questionId] = answers.value;
  document.getElementById(labelId).style.fontWeight = "bold";
  disableQuestion(classId);
  showResult(questionId);
  answered++;
};

var submitAnswer = function() {
   var answer = document.getElementById(submitq1);
   userAnswers.push[answer];
};
  console.log(userAnswers); 

function showScore() {
  if(answered != answered.length) {
    alert("You have not answered all of the questions yet!");
    return false;
  }
 var questionCount = answered.length;
  var correct = 0;
 var incorrect = 0;
  for(var i=0;i<questionCount;i++) {
    if(userAnswers[i] == answered[i])
      correct++;
    else
      incorrect++;
  }
  var response = ["10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "0"];
   var pc = Math.round((correct / questionCount) * 100);
  var alertMsg = "You scored " + correct + " out of " + "10" + "\n\n";
  if(pc == 100)
    alertMsg += response[0];
  else if(pc >= 90)
    alertMsg += response[1];
  else if(pc>= 80)
    alertMsg += response[2];
  else if(pc >= 70)
    alertMsg += response[3];
  else if(pc > 60)
    alertMsg += response[4];
  else if(pc >= 50)
    alertMsg += response[5];
  else if(pc >= 40)
    alertMsg += response[6];
  else if(pc >= 30)
    alertMsg += response[7];
  else if(pc>= 20)
    alertMsg += response[8];
  else if(pc >= 10)
    alertMsg += response[9];
  else if(pc >= 0)
    alertMsg += response[10];
  if(pc < 100) {
    if(confirm(alertMsg))
      resetQuiz(false);
    else
      return false;
  } else {
    alert(alertMsg);
  }
}
function disableQuestion(classId) {
  var alltags=document.all? document.all : document.getElementsByTagName("*");
  for (var i=0; i<alltags.length; i++) {

    if (alltags[i].className == classId) {
      alltags[i].disabled = true;
    }
  }
};
var userAnswers=[];
var=0;
变量问题=[问题1、问题2、问题3、问题4、问题5、问题6、问题7、问题8、问题9、问题10];
var答案=[q1a、q1b、q1c、q1d、q2a、q2b、q2c、q2d、q3a、q3b、q3c、q4a、q4b、q4c、q4d、q5a、q5b、q5c、q5d、q6a、q6b、q6c、q6d、q7a、q7b、q7c、q7d];
功能重置测验(showConfirm){
如果(显示确认)
如果(!确认(“是否确实要重置测验并从头开始?”)
返回false;
document.location=document.location;
}
var submitAnswer=函数(问题ID、obj、classId、labelId){
userAnswers[questionId]=answers.value;
document.getElementById(labelId).style.fontwweight=“bold”;
残疾问题(classId);
显示结果(问题ID);
回答++;
};
var submitAnswer=函数(){
var answer=document.getElementById(submitq1);
userAnswers.push[answer];
};
日志(userAnswers);
函数showScore(){
如果(已回答!=已回答的长度){
警惕(“您尚未回答所有问题!”);
返回false;
}
var questionCount=回答的长度;
var=0;
var=0;
对于(变量i=0;i=90)
alertMsg+=响应[1];
否则,如果(pc>=80)
alertMsg+=响应[2];
否则,如果(pc>=70)
alertMsg+=响应[3];
否则,如果(pc>60)
alertMsg+=响应[4];
否则,如果(pc>=50)
alertMsg+=响应[5];
否则,如果(pc>=40)
alertMsg+=响应[6];
否则,如果(pc>=30)
alertMsg+=响应[7];
否则,如果(pc>=20)
alertMsg+=响应[8];
否则,如果(pc>=10)
alertMsg+=响应[9];
如果(pc>=0),则为else
alertMsg+=响应[10];
如果(pc<100){
如果(确认(alertMsg))
重置测验(假);
其他的
返回false;
}否则{
警报(alertMsg);
}
}
功能禁用问题(classId){
var alltags=document.all?document.all:document.getElementsByTagName(“*”);

对于(var i=0;i我不确定为什么它说submitAnswer不是一个函数,但您定义了两个同名函数,这在JS中是不允许的。
请尝试重命名其中一个或根据建议更改参数列表,如果问题仍然存在,请发布更多信息。

之所以说submitAnswer不是函数,是因为您已将其定义为变量,因此JS无法正确提升它。而不是使用

var submitAnswer= function(blah){blah}
只用

function submitAnswer(blah){blah}

另外,请确保您似乎已经声明了submitAnswer两次。请确保使用了正确的submitAnswer。

Javascript中的函数通常以以下方式定义:

function submitAnswer(){
    //code here
}

然后可以在声明后调用。如果有多个同名函数,可以通过为每个函数提供不同的参数列表来重载它。

这里有几个问题需要解决,我将尝试指出最严重的问题:

首先,您有函数submitAnswer的重复声明:

var submitAnswer = function(questionId, obj, classId, labelId) {
  userAnswers[questionId] = answers.value;
  document.getElementById(labelId).style.fontWeight = "bold";
  disableQuestion(classId);
  showResult(questionId);
  answered++;
};

var submitAnswer = function() {
   var answer = document.getElementById(submitq1);
   userAnswers.push[answer];
};
我猜你只想要其中一个,当你这样写的时候,第二个会覆盖第一个。我猜这是你想要保留的第一个,因为它看起来是最完整的

第二,你的问题6有错误的ID

<ul class="answers" id="question6">
  <input type="radio" name="q6" value="a" id="q5a">a. scan the sides of the road ahead for animals</label><br/>
  <input type="radio" name="q6" value="b" id="q5b">b. watch for animal crossing signs</label><br/>
  <input type="radio" name="q6" value="c" id="q5c">c. be extra cautious at dusk and dawn</label><br/>
  <input type="radio" name="q6" value="d" id="q6d">d. all of the above</label><br/>
</ul>
    a、 扫视前方道路两侧是否有动物
    b、 注意动物交叉标志
    c、 黄昏和黎明时要格外小心
    d、 以上所有内容
如果要循环此数组以将其与id匹配,则无法获得正确的元素,请将id更改为“q6[a | b | c | d]”

当您解决了这个问题后,下一步将是创建submitAnswer函数,从您的示例来看,您希望它如何工作还不清楚

好的第一步是将视图/html结构与逻辑/JavaScript分离,并了解参数是如何传递给函数的。我对您的代码进行了一些清理,为您提供了这段代码中的垫脚石:

var userAnswers=[];
var=0;
变量问题=[问题1、问题2、问题3、问题4、问题5、问题6、问题7、问题8、问题9、问题10];
var答案=[q1a、q1b、q1c、q1d、q2a、q2b、q2c、q2d、q3a、q3b、q3c、q4a、q4b、q4c、q4d、q5a、q5b、q5c、q5d、q6a、q6b、q6c、q6d、q7a、q7b、q7c、q7d];
功能重置测验(showConfirm){
如果(显示确认)
如果(!确认(“是否确实要重置测验并从头开始?”)
返回false;
document.location=document.location;
}
var submitAnswer=函数(){
日志(“提交的答案!”);
};
函数showScore(){
如果(已回答!=已回答的长度){
警惕(“您尚未回答所有问题!”);
返回false;
}
var questionCount=回答的长度;
var=0;
var=0;
对于(变量i=0;i=90)
alertMsg+=响应[1];
否则,如果(pc>=80)
alertMsg+=响应[2];
否则,如果(pc>=70)
alertMsg+=响应[3];
否则,如果(pc>60)
alertMsg+=响应[4];
否则,如果(pc>=50)
alertMsg+=响应[5];
否则,如果(pc>=40)
alertMsg+=响应[6];
否则,如果(pc>=30)
alertMsg+=响应[7];
否则,如果(pc>=20)
alertMsg+=响应[8];
否则,如果(pc>=10)
alertMsg+=响应[9];
如果(pc>=0),则为else
alertMsg+=响应[10];
如果(pc<100){
如果(确认(alertMsg))
重置测验(假);
其他的
返回false;
}否则{
警报(alertMsg);
}
}
功能禁用问题(classId){
v
<ul class="answers" id="question6">
  <input type="radio" name="q6" value="a" id="q5a">a. scan the sides of the road ahead for animals</label><br/>
  <input type="radio" name="q6" value="b" id="q5b">b. watch for animal crossing signs</label><br/>
  <input type="radio" name="q6" value="c" id="q5c">c. be extra cautious at dusk and dawn</label><br/>
  <input type="radio" name="q6" value="d" id="q6d">d. all of the above</label><br/>
</ul>