javascript 使用javascript的多项选择测验

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20167643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:55:30  来源:igfitidea点击:

Multiple choice quiz using javascript

javascripthtmlarrays

提问by user3014211

I have created a multiple choice quiz that gives a percentage result that works but the percentage is consistently incorrect. Not sure how to correct this exactly, as I've tried various approaches and nothing seems to work.

我创建了一个多项选择测验,它给出了一个有效的百分比结果,但该百分比始终不正确。不知道如何准确地纠正这个问题,因为我尝试了各种方法,但似乎没有任何效果。

Script:
var numQues = 5;
var numChoi = 3;
var answers = new Array(5);
answers[0] = "David Bowie";
answers[1] = "AM";
answers[2] = "Australia";
answers[3] = "Boneface";
answers[4] = "Sound City";
function getScore(form) {
  var score = 0;
  var currElt;
  var currSelection;
  for (i=0; i<numQues; i++) {
    currElt = i*numChoi;
    for (j=0; j<numChoi; j++) {
      currSelection = form.elements[currElt + j];
      if (currSelection.checked) {
        if (currSelection.value == answers[i]) {
          score++;
          break;
        }
      }
    }
  }

  score = Math.round(score/numQues*100);
  form.percentage.value = score + "%";
  var correctAnswers = "";
  for (i=1; i<=numQues; i++) {
    correctAnswers += i + ". " + answers[i-1] + "\r\n";
  }
  form.solutions.value = correctAnswers;
}

HTML:

<center>
<h1>Test your music knowledge!</h1>
<p>
<form name="quiz">
<p>
<b>1. Who supplied a cameo vocal for the Arcade Fire song, "Reflektor"?<br></b>
<blockquote>
<input type="radio" name="q1" value="Elton John">Elton John<br>
<input type="radio" name="q1" value="David Bowie">David Bowie<br>
<input type="radio" name="q1" value="Leonard Cohen">Leonard Cohen<br>
</blockquote>
<p><b>
2. What is the title of Arctic Monkeys 2013 album?<br></b>
<blockquote>
<input type="radio" name="q2" value="AM">AM<br>
<input type="radio" name="q2" value="AM I?">AM I?<br>
<input type="radio" name="q2" value="SAM I AM">SAM I AM<br>
</blockquote>
<p><b>
3. Where do psychedelic rockers Tame Impala hail from?<br></b>
<blockquote>
<input type="radio" name="q3" value="Australia">Australia<br>
<input type="radio" name="q3" value="New Zealand">New Zealand<br>
<input type="radio" name="q3" value="America">America<br>
</blockquote>
<p><b>
4. Which artist designed Queens Of The Stone Ages "...Like Clockwork" album artwork?<br></b>
<blockquote>
<input type="radio" name="q4" value="Banksy">Banksy<br>
<input type="radio" name="q4" value="Bono">Bono<br>
<input type="radio" name="q4" value="Boneface">Boneface<br>
</blockquote>
<p><b>
5. What was the name of Dave Grohls 2013 rockumentary?<br></b>
<blockquote>
<input type="radio" name="q5" value="Sin City">Sin City<br>
<input type="radio" name="q5" value="Sound City">Sound City<br>
<input type="radio" name="q5" value="Oh, I'm just so PRETTY.">Oh, I'm just so PRETTY<br>
</blockquote>
<p><b>

<input type="button" value="Get score" onClick="getScore(this.form)">
<input type="reset" value="Clear"><p>
Score = <input type=text size=15 name="percentage"><br>
Correct answers:<br></font>
<textarea name="solutions" wrap="virtual" rows="4" cols="25"></textarea>
</form>
<p>

Any help appreciated!

任何帮助表示赞赏!

回答by php_nub_qq

All you need to do is get the number of correct answers and multiply by 20.

您需要做的就是获得正确答案的数量并乘以 20。

Percentage = ( 100 / Number of questions ) * Number of right answers

Percentage = ( 100 / Number of questions ) * Number of right answers

EDIT:

编辑

I just tested your code and it works?

我刚刚测试了你的代码,它有效吗?

回答by pete

Given your posted fiddle...

鉴于你张贴的小提琴......

There are technically two problems (but one's a real simple fix).

从技术上讲有两个问题(但一个是真正简单的修复)。

The JavaScript in the fiddle is set to run onload. This leaves your functions undefined in a global scope as jsFiddle wraps this in an anonymous function window.onload=function(){... your code ...}. The real simple fix for this is to either:

小提琴中的 JavaScript 设置为 run onload。这会使您的函数在全局范围内未定义,因为 jsFiddle 将其包装在匿名函数中window.onload=function(){... your code ...}。真正简单的解决方法是:

  1. Not use the varkeyword to define your global variables (notrecommended).
  2. Set the jsFiddle to run the JavaScript either in No wrap - in <head>or No wrap - in <body>(recommended).
  1. 不要使用var关键字来定义全局变量(推荐)。
  2. 将 jsFiddle 设置为在No wrap - in <head>No wrap - in <body>(推荐)中运行 JavaScript 。

The real problem is how you're accessing the form elements via the double loop:

真正的问题是如何通过双循环访问表单元素:

for (i = 0; i < numQues; i++) { // where numQues = 5
  currElt = i * numChoi;        // where numChoi = 3
  for (j = 0; j < numChoi; j++) {
      currSelection = form.elements[currElt + j];
      if (currSelection.checked) {
          if (currSelection.value == answers[i]) {
              score++;
              break;
          }
      }
  }
}

when combined with how you've defined the form elements (see inline comments):

结合您定义表单元素的方式(请参阅内联注释):

<input type="text" name="numbers" ...>                  // currSelection = 0,  numQues = 0, numChoi = 0, answer[i] = "David Bowie"
<button type="button" onclick="">Off you go!</button>   // currSelection = 1,  numQues = 0, numChoi = 1, answer[i] = "David Bowie"
<input type="radio" name="q1" value="Elton John">       // currSelection = 2,  numQues = 0, numChoi = 2, answer[i] = "David Bowie"
<input type="radio" name="q1" value="David Bowie">      // currSelection = 3,  numQues = 1, numChoi = 0, answer[i] = "AM"
<input type="radio" name="q1" value="Leonard Cohen">    // currSelection = 4,  numQues = 1, numChoi = 1, answer[i] = "AM"
<input type="radio" name="q2" value="AM">               // currSelection = 5,  numQues = 1, numChoi = 2, answer[i] = "AM"
<input type="radio" name="q2" value="AM I?">            // currSelection = 6,  numQues = 2, numChoi = 0, answer[i] = "Australia"
<input type="radio" name="q2" value="SAM I AM">         // currSelection = 7,  numQues = 2, numChoi = 1, answer[i] = "Australia"
<input type="radio" name="q3" value="Australia">        // currSelection = 8,  numQues = 2, numChoi = 2, answer[i] = "Australia"
<input type="radio" name="q3" value="New Zealand">      // currSelection = 9,  numQues = 3, numChoi = 0, answer[i] = "Boneface"
<input type="radio" name="q3" value="America">          // currSelection = 11, numQues = 3, numChoi = 1, answer[i] = "Boneface"
<input type="radio" name="q4" value="Banksy">           // currSelection = 12, numQues = 3, numChoi = 2, answer[i] = "Boneface"
<input type="radio" name="q4" value="Bono">             // currSelection = 13, numQues = 4, numChoi = 0, answer[i] = "Sound City"
<input type="radio" name="q4" value="Boneface">         // currSelection = 14, numQues = 4, numChoi = 1, answer[i] = "Sound City"
<input type="radio" name="q5" value="Sin City">         // currSelection = 14, numQues = 4, numChoi = 2, answer[i] = "Sound City"; Looping ends
<input type="radio" name="q5" value="Sound City">
<input type="radio" name="q5" value="Oh, I'm just so PRETTY.">
<input type="button" value="Get score" onclick="getScore(this.form)">
<input type="reset" value="Clear">
<input type="text" size="15" name="percentage">
<textarea name="solutions" wrap="virtual" rows="4" cols="25"></textarea>

This causes the first question to be effectively skipped and the last question to never be correct, so the user can only obtain a maximum score of 60%. There are multiple solutions to this problem.

这导致第一个问题被有效跳过,最后一个问题永远不会正确,因此用户只能获得 60% 的最高分数。这个问题有多种解决方案。

  1. Change your markup so that onlyyour "answer" inputs are in the form. This way the double-loop will work.
  2. Don't use a double-loop where a single-loop will do:

    for (i = 0; i < form.elements.length; i++) { // this is work regardless of how many elements are in the form. currSelection = form.elements[i]; if (currSelection.checked) { if (answers.indexOf(currSelection.value) > -1) { score++; } } }

  1. 更改您的标记,以便只有您的“答案”输入出现在表单中。这样双循环就可以工作了。
  2. 不要使用单循环的双循环:

    for (i = 0; i < form.elements.length; i++) { // 无论表单中有多少元素,这都是有效的。currSelection = form.elements[i]; if (currSelection.checked) { if (answers.indexOf(currSelection.value) > -1) { score++; } } }

As for other solutions, they all boil down to "change both your markup and your JavaScript so that they play more nicely together (and are perhaps more readable)". I would go on, but then this answer go well outside the scope of your initial question.

至于其他解决方案,它们都归结为“改变你的标记和你的 JavaScript,让它们更好地协同工作(并且可能更具可读性)”。我会继续,但是这个答案远远超出了您最初问题的范围。

I put together some examples of how to "change both your markup and your JavaScript so that they play more nicely together (and are perhaps more readable)" at http://jsfiddle.net/C2Bqh/1/and http://jsfiddle.net/C2Bqh/2/.

我在http://jsfiddle.net/C2Bqh/1/http://jsfiddle 上汇总了一些关于如何“更改标记和 JavaScript 以使它们更好地协同工作(并且可能更具可读性)”的示例.net/C2Bqh/2/