使用javascript进行多项选择测验

时间:2013-11-23 20:43:28

标签: javascript html arrays

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

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>

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:1)

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

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

修改

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

答案 1 :(得分:1)

鉴于您发布了fiddle ...

技术上存在两个问题(但一个是一个非常简单的修复)。

小提琴中的JavaScript设置为运行onload。这使得函数在全局范围内未定义,因为jsFiddle将其包装在匿名函数window.onload=function(){... your code ...}中。对此的真正简单修复是:

  1. 不使用var关键字来定义全局变量(不是 推荐)。
  2. 设置jsFiddle以在No wrap - in <head>No wrap - in <body>(推荐)中运行JavaScript。
  3. 真正的问题是你如何通过双循环访问表单元素:

    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;
              }
          }
      }
    }
    

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

    <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>
    

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

    1. 更改您的标记,以便您的“答案”输入位于 形成。这样双循环就可以了。
    2. 不要使用单循环的双循环:

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

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

      我汇总了一些示例,说明如何“改变你的标记和你的JavaScript,以便它们在http://jsfiddle.net/C2Bqh/1/http://jsfiddle.net/C2Bqh/2/上更好地一起玩(并且可能更具可读性)。