Tidey
09-09-2011, 06:34 AM
Good evening!
After spending the last few days wrecking my brain over a simple JavaScript quiz I decided it was time I asked for help.
The complete code is right beneath this post. The page will display brief instructions followed by a table containing the quiz.
I'm having two rather peculiar problems.
The first is with the Score() function which reads as follows:
function Score(){
for(i=1;i<=10;i++)
if(ans[i]== yourAns[i])
score++;
prequiz.score.value= score;
}
Now unless I'm mistaken (which I am since the score isn't being tabulated correctly) this function essentially uses a for to cycle through both arrays, comparing the user's input against the array containing the right answers. If the contents within index i are the same on both arrays the score increases by one point. Once it cycles through the arrays, it passes score over to prequiz.score.value which will later pass this number to the text box that will display the grade to the user.
The problem I'm experiencing is that the score will either be stuck at 2 or will display as 9 (even though all 10 answers were correct).
Steps I've taken to try to solve this:
Thinking it was the setAnsArray(question, answer) function I added a document.writeline to it. However it showed that the values were being passed correctly to the answer array.
Added a document.writeline to the Score() function. Unlike the point above, score always displays as 00000 regardless of how many right answers I provide.
The second issue I'm having is that the reset button is not working correctly. If I hit reset and answer the first question, the score will come back as double the score displayed before.
Any insight into what is causing this issue is greatly appreciated!
<?xml version="1.0" encoding= "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns= "http://www.w3.org/1999/xhtml">
<head>
<title>Pre-Quiz!</title>
<script type="text/JavaScript">
var ans= new Array(10);
var yourAns= new Array(10);
var score= 0;
ans[0] = "b";
ans[1] = "c";
ans[2] = "c";
ans[3] = "a";
ans[4] = "d";
ans[5] = "c";
ans[6] = "c";
ans[7] = "a";
ans[8] = "b";
ans[9] = "c";
function setAnsArray(question, answer) {
yourAns[question]= answer;
}
function Score(){
for(i=1;i<=10;i++)
if(ans[i]== yourAns[i])
score++;
prequiz.score.value= score;
}
</script>
</head>
<body>
<h1>Pre-Quiz</h1>
<p>The following test will allow you to evaluate your knowledge in programming prior to starting the course.
After completing the test you will receive a score. Should you have any further doubts feel free to bring them
with you on the first day of class.</p>
</body>
<form name="prequiz" action="JavaScript:Score()">
<center>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value= "Reset" /><br><br>
</center>
<b>Score:</b><input type="text" name="score" disabled=true><br>
<hr>
<center><table border="3" width="65%"
summary= "Questions and options are organized in a table.">
<thead>
<tr>
<th>Question</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>1. The following parameter code is used in which control structure?</b><br>
(int i=0; i<= size; i++)<br> </td>
<td><input type="radio" name="q1" value="a" onClick="setAnsArray(1, this.value)">a. If<br>
<input type="radio" name="q1" value="b" onClick="setAnsArray(1, this.value)">b. For<br>
<input type="radio" name="q1" value="c" onClick="setAnsArray(1, this.value)">c. Do-while<br>
<input type="radio" name="q1" value="d" onClick="setAnsArray(1, this.value)">d. While</td>
</tr>
<tr>
<td><b>2. What does the command "cout" do?</b><br></td>
<td><input type="radio" name="q2" value="a" onClick="setAnsArray(2, this.value)">a. Obtains information from the user.<br>
<input type="radio" name="q2" value="b" onClick="setAnsArray(2, this.value)">b. Closes the window.<br>
<input type="radio" name="q2" value="c" onClick="setAnsArray(2, this.value)">c. Displays on screen.<br>
<input type="radio" name="q2" value="d" onClick="setAnsArray(2, this.value)">d. Pauses a process.</td>
</tr>
<tr>
<td><b>3. What is the origin of name Python? (referring to the programming language)</b><br></td>
<td><input type="radio" name="q3" value="a" onClick="setAnsArray(3, this.value)">a. Creator had a pet python.<br>
<input type="radio" name="q3" value="b" onClick="setAnsArray(3, this.value)">b. Named after a student of Plato.<br>
<input type="radio" name="q3" value="c" onClick="setAnsArray(3, this.value)">c. Named after Monty Python's Flying Circus.<br>
<input type="radio" name="q3" value="d" onClick="setAnsArray(3, this.value)">d. Creator was a fan of Greek mythology.</td>
</tr>
<tr>
<td><b>4. What does HTML stand for?</b><br></td>
<td><input type="radio" name="q4" value="a" onClick="setAnsArray(4, this.value)">a. HyperText Markup Language<br>
<input type="radio" name="q4" value="b" onClick="setAnsArray(4, this.value)">b. Hyper Tools for Managing Languages<br>
<input type="radio" name="q4" value="c" onClick="setAnsArray(4, this.value)">c. Heap Text Manager Lead<br>
<input type="radio" name="q4" value="d" onClick="setAnsArray(4, this.value)">d. Horde Take Much Lore</td>
</tr>
<tr>
<td><b>5. Who invented JavaScript?</b><br></td>
<td><input type="radio" name="q5" value="a" onClick="setAnsArray(5, this.value)">a. Bill Gates<br>
<input type="radio" name="q5" value="b" onClick="setAnsArray(5, this.value)">b. Isaac Newton<br>
<input type="radio" name="q5" value="c" onClick="setAnsArray(5, this.value)">c. Elvis Java<br>
<input type="radio" name="q5" value="d" onClick="setAnsArray(5, this.value)">d. Netscape</td>
</tr>
<tr>
<td><b>6. What is a variable?</b><br></td>
<td><input type="radio" name="q6" value="a" onClick="setAnsArray(6, this.value)">a. An unknown.<br>
<input type="radio" name="q6" value="b" onClick="setAnsArray(6, this.value)">b. A value that changes.<br>
<input type="radio" name="q6" value="c" onClick="setAnsArray(6, this.value)">c. The name for a place in the computer's memory where a certain data is stored.<br>
<input type="radio" name="q6" value="d" onClick="setAnsArray(6, this.value)">d. Part of an algebraic expression.</td>
</tr>
<tr>
<td><b>7. What is a compiler?</b><br></td>
<td><input type="radio" name="q7" value="a" onClick="setAnsArray(7, this.value)">a. Converts the target file into a .zip .<br>
<input type="radio" name="q7" value="b" onClick="setAnsArray(7, this.value)">b. Gathers all documents in one.<br>
<input type="radio" name="q7" value="c" onClick="setAnsArray(7, this.value)">c. A program or set of programs that transforms source code from programming language into another computer language.<br>
<input type="radio" name="q7" value="d" onClick="setAnsArray(7, this.value)">d. Secret ingridient in KFC's chicken</td>
</tr>
<tr>
<td><b>8. Define portability:</b><br></td>
<td><input type="radio" name="q8" value="a" onClick="setAnsArray(8, this.value)">a. Characteristic attributed to a program if it can be used in another OS different from the one it was created in without requiring major rework.<br>
<input type="radio" name="q8" value="b" onClick="setAnsArray(8, this.value)">b. Ease to transport the program's disks.<br>
<input type="radio" name="q8" value="c" onClick="setAnsArray(8, this.value)">c. How big the size of the program is.<br>
<input type="radio" name="q8" value="d" onClick="setAnsArray(8, this.value)">d. Time required to download a program.</td>
</tr>
<tr>
<td><b>9. What is an executable file?</b><br></td>
<td><input type="radio" name="q9" value="a" onClick="setAnsArray(9, this.value)">a. A text document containing code.<br>
<input type="radio" name="q9" value="b" onClick="setAnsArray(9, this.value)">b. A file in a format the computar can directly execute.<br>
<input type="radio" name="q9" value="c" onClick="setAnsArray(9, this.value)">c. A virus.<br>
<input type="radio" name="q9" value="d" onClick="setAnsArray(9, this.value)">d. A malicious program.</td>
</tr>
<tr>
<td><b>10. Which of the following is not a programming language? </b><br></td>
<td><input type="radio" name="q10" value="a" onClick="setAnsArray(10, this.value)">a. HTML<br>
<input type="radio" name="q10" value="b" onClick="setAnsArray(10, this.value)">b. Lua<br>
<input type="radio" name="q10" value="c" onClick="setAnsArray(10, this.value)">c. Moolah<br>
<input type="radio" name="q10" value="d" onClick="setAnsArray(10, this.value)">d. C++</td>
</tr>
</tbody>
</table></center>
</form>
</html>
After spending the last few days wrecking my brain over a simple JavaScript quiz I decided it was time I asked for help.
The complete code is right beneath this post. The page will display brief instructions followed by a table containing the quiz.
I'm having two rather peculiar problems.
The first is with the Score() function which reads as follows:
function Score(){
for(i=1;i<=10;i++)
if(ans[i]== yourAns[i])
score++;
prequiz.score.value= score;
}
Now unless I'm mistaken (which I am since the score isn't being tabulated correctly) this function essentially uses a for to cycle through both arrays, comparing the user's input against the array containing the right answers. If the contents within index i are the same on both arrays the score increases by one point. Once it cycles through the arrays, it passes score over to prequiz.score.value which will later pass this number to the text box that will display the grade to the user.
The problem I'm experiencing is that the score will either be stuck at 2 or will display as 9 (even though all 10 answers were correct).
Steps I've taken to try to solve this:
Thinking it was the setAnsArray(question, answer) function I added a document.writeline to it. However it showed that the values were being passed correctly to the answer array.
Added a document.writeline to the Score() function. Unlike the point above, score always displays as 00000 regardless of how many right answers I provide.
The second issue I'm having is that the reset button is not working correctly. If I hit reset and answer the first question, the score will come back as double the score displayed before.
Any insight into what is causing this issue is greatly appreciated!
<?xml version="1.0" encoding= "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns= "http://www.w3.org/1999/xhtml">
<head>
<title>Pre-Quiz!</title>
<script type="text/JavaScript">
var ans= new Array(10);
var yourAns= new Array(10);
var score= 0;
ans[0] = "b";
ans[1] = "c";
ans[2] = "c";
ans[3] = "a";
ans[4] = "d";
ans[5] = "c";
ans[6] = "c";
ans[7] = "a";
ans[8] = "b";
ans[9] = "c";
function setAnsArray(question, answer) {
yourAns[question]= answer;
}
function Score(){
for(i=1;i<=10;i++)
if(ans[i]== yourAns[i])
score++;
prequiz.score.value= score;
}
</script>
</head>
<body>
<h1>Pre-Quiz</h1>
<p>The following test will allow you to evaluate your knowledge in programming prior to starting the course.
After completing the test you will receive a score. Should you have any further doubts feel free to bring them
with you on the first day of class.</p>
</body>
<form name="prequiz" action="JavaScript:Score()">
<center>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value= "Reset" /><br><br>
</center>
<b>Score:</b><input type="text" name="score" disabled=true><br>
<hr>
<center><table border="3" width="65%"
summary= "Questions and options are organized in a table.">
<thead>
<tr>
<th>Question</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>1. The following parameter code is used in which control structure?</b><br>
(int i=0; i<= size; i++)<br> </td>
<td><input type="radio" name="q1" value="a" onClick="setAnsArray(1, this.value)">a. If<br>
<input type="radio" name="q1" value="b" onClick="setAnsArray(1, this.value)">b. For<br>
<input type="radio" name="q1" value="c" onClick="setAnsArray(1, this.value)">c. Do-while<br>
<input type="radio" name="q1" value="d" onClick="setAnsArray(1, this.value)">d. While</td>
</tr>
<tr>
<td><b>2. What does the command "cout" do?</b><br></td>
<td><input type="radio" name="q2" value="a" onClick="setAnsArray(2, this.value)">a. Obtains information from the user.<br>
<input type="radio" name="q2" value="b" onClick="setAnsArray(2, this.value)">b. Closes the window.<br>
<input type="radio" name="q2" value="c" onClick="setAnsArray(2, this.value)">c. Displays on screen.<br>
<input type="radio" name="q2" value="d" onClick="setAnsArray(2, this.value)">d. Pauses a process.</td>
</tr>
<tr>
<td><b>3. What is the origin of name Python? (referring to the programming language)</b><br></td>
<td><input type="radio" name="q3" value="a" onClick="setAnsArray(3, this.value)">a. Creator had a pet python.<br>
<input type="radio" name="q3" value="b" onClick="setAnsArray(3, this.value)">b. Named after a student of Plato.<br>
<input type="radio" name="q3" value="c" onClick="setAnsArray(3, this.value)">c. Named after Monty Python's Flying Circus.<br>
<input type="radio" name="q3" value="d" onClick="setAnsArray(3, this.value)">d. Creator was a fan of Greek mythology.</td>
</tr>
<tr>
<td><b>4. What does HTML stand for?</b><br></td>
<td><input type="radio" name="q4" value="a" onClick="setAnsArray(4, this.value)">a. HyperText Markup Language<br>
<input type="radio" name="q4" value="b" onClick="setAnsArray(4, this.value)">b. Hyper Tools for Managing Languages<br>
<input type="radio" name="q4" value="c" onClick="setAnsArray(4, this.value)">c. Heap Text Manager Lead<br>
<input type="radio" name="q4" value="d" onClick="setAnsArray(4, this.value)">d. Horde Take Much Lore</td>
</tr>
<tr>
<td><b>5. Who invented JavaScript?</b><br></td>
<td><input type="radio" name="q5" value="a" onClick="setAnsArray(5, this.value)">a. Bill Gates<br>
<input type="radio" name="q5" value="b" onClick="setAnsArray(5, this.value)">b. Isaac Newton<br>
<input type="radio" name="q5" value="c" onClick="setAnsArray(5, this.value)">c. Elvis Java<br>
<input type="radio" name="q5" value="d" onClick="setAnsArray(5, this.value)">d. Netscape</td>
</tr>
<tr>
<td><b>6. What is a variable?</b><br></td>
<td><input type="radio" name="q6" value="a" onClick="setAnsArray(6, this.value)">a. An unknown.<br>
<input type="radio" name="q6" value="b" onClick="setAnsArray(6, this.value)">b. A value that changes.<br>
<input type="radio" name="q6" value="c" onClick="setAnsArray(6, this.value)">c. The name for a place in the computer's memory where a certain data is stored.<br>
<input type="radio" name="q6" value="d" onClick="setAnsArray(6, this.value)">d. Part of an algebraic expression.</td>
</tr>
<tr>
<td><b>7. What is a compiler?</b><br></td>
<td><input type="radio" name="q7" value="a" onClick="setAnsArray(7, this.value)">a. Converts the target file into a .zip .<br>
<input type="radio" name="q7" value="b" onClick="setAnsArray(7, this.value)">b. Gathers all documents in one.<br>
<input type="radio" name="q7" value="c" onClick="setAnsArray(7, this.value)">c. A program or set of programs that transforms source code from programming language into another computer language.<br>
<input type="radio" name="q7" value="d" onClick="setAnsArray(7, this.value)">d. Secret ingridient in KFC's chicken</td>
</tr>
<tr>
<td><b>8. Define portability:</b><br></td>
<td><input type="radio" name="q8" value="a" onClick="setAnsArray(8, this.value)">a. Characteristic attributed to a program if it can be used in another OS different from the one it was created in without requiring major rework.<br>
<input type="radio" name="q8" value="b" onClick="setAnsArray(8, this.value)">b. Ease to transport the program's disks.<br>
<input type="radio" name="q8" value="c" onClick="setAnsArray(8, this.value)">c. How big the size of the program is.<br>
<input type="radio" name="q8" value="d" onClick="setAnsArray(8, this.value)">d. Time required to download a program.</td>
</tr>
<tr>
<td><b>9. What is an executable file?</b><br></td>
<td><input type="radio" name="q9" value="a" onClick="setAnsArray(9, this.value)">a. A text document containing code.<br>
<input type="radio" name="q9" value="b" onClick="setAnsArray(9, this.value)">b. A file in a format the computar can directly execute.<br>
<input type="radio" name="q9" value="c" onClick="setAnsArray(9, this.value)">c. A virus.<br>
<input type="radio" name="q9" value="d" onClick="setAnsArray(9, this.value)">d. A malicious program.</td>
</tr>
<tr>
<td><b>10. Which of the following is not a programming language? </b><br></td>
<td><input type="radio" name="q10" value="a" onClick="setAnsArray(10, this.value)">a. HTML<br>
<input type="radio" name="q10" value="b" onClick="setAnsArray(10, this.value)">b. Lua<br>
<input type="radio" name="q10" value="c" onClick="setAnsArray(10, this.value)">c. Moolah<br>
<input type="radio" name="q10" value="d" onClick="setAnsArray(10, this.value)">d. C++</td>
</tr>
</tbody>
</table></center>
</form>
</html>