[英]Multiple choice quiz using javascript
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! 任何帮助赞赏!
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
EDIT : 编辑 :
I just tested your code and it works? 我刚刚测试了你的代码,它有效吗?
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
. 小提琴中的JavaScript设置为运行onload
。 This leaves your functions undefined in a global scope as jsFiddle wraps this in an anonymous function window.onload=function(){... your code ...}
. 这使得函数在全局范围内未定义,因为jsFiddle将其包装在匿名函数window.onload=function(){... your code ...}
。 The real simple fix for this is to either: 对此的真正简单修复是:
var
keyword to define your global variables ( not recommended). 不使用var
关键字来定义全局变量( 不推荐)。 No wrap - in <head>
or No wrap - in <body>
(recommended). 设置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%. 这导致第一个问题被有效地跳过,而最后一个问题永远不会正确,因此用户只能获得60%的最高分数。 There are multiple solutions to this problem. 这个问题有多种解决方案。
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. for(i = 0; i <form.elements.length; i ++){//无论表单中有多少元素,这都是可行的。 currSelection = form.elements[i]; currSelection = form.elements [i]; if (currSelection.checked) { if (answers.indexOf(currSelection.value) > -1) { score++; 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)". 至于其他解决方案,它们都归结为“改变你的标记和你的JavaScript,以便它们更好地一起播放(并且可能更具可读性)”。 I would go on, but then this answer go well outside the scope of your initial question. 我会继续,但是这个答案远远超出了你最初问题的范围。
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 / 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.