...

View Full Version : Help about creating a Quiz in Javascript!



joanmicham
09-30-2012, 03:43 AM
Hello i have an assignment about creating a quiz in javascript which will display the score at the end and then an alert with the wrong answers. I have this code but i don't know what is going wrong. Can you help me please?


<html>
<head>
<title>Ioanna Michael - Personal Portfolio</title>
<link rel="stylesheet" type="text/css" href="im.michael.css" />
<script>
var totalquestions=4;
var actualchoices=new Array()
var correctchoices=new Array()
correctchoices[0]='b'
correctchoices[1]='c'
correctchoices[2]='b'
correctchoices[3]='c'
function getScore(){
var incorrect=null
for (i=1;i<=totalquestions;i++){
var thequestion=eval("document.immichael.question"+i)
for (j=0;j<thequestion.length;j++){
if (thequestion[j].checked==true)
actualchoices[i]=thequestion[j].value
}

if (actualchoices[i]!=correctchoices[i]){
if (incorrect==null)
incorrect=i
else
incorrect+="/"+i
}
}
}
</script>
</head>

<body>
<div id="table">
<table width="1000px" border="0">
<tr>
<th colspan="2"><img src="header.jpg" width="1000px"/></th>
</tr>
<tr>
<td class="menu">
<p><a href="http://www.youtube.com/watch?v=EAfaIAnSOjc&feature=player_embedded">3d Modelling</a></p>
<p><a href="http://www.youtube.com/watch?v=uEvOuhefOks">Video Production</a></p>
<p><a href="http://www.facebook.com/ioanna.michael1">Contact Us</a></p>
<div id="info">
<p style="color:white;"><img src="facebook_logo_2.jpg" width="20px"/>&nbsp;&nbsp;ioanna.michael1</p>
<p style="color:white;"><img src="phone-rotary-symbol.png" width="20px"/>&nbsp;&nbsp;96 844741</p>
</div>
</td>
<td>
<div id="quiz">
<form method="POST" name="immichael">
<h2><b>Quiz</b></h2>
<ol>
<li>Ti simainei CSS?</li>
<ul>
<li><input type="radio" name="css" value="a">Colourful Style Symbols</input></li>
<li><input type="radio" name="css" value="b">Cascading Style Sheets</input></li>
<li><input type="radio" name="css" value="c">Computer Style Symbols</input></li>
</ul>
<li>Ti simainei HTML?</li>
<ul>
<li><input type="radio" name="html" value="a">Hypno-tic markup language</input></li>
<li><input type="radio" name="html" value="b">Hyper-text mathematical language</input></li>
<li><input type="radio" name="html" value="c">Hyper-text markup language</input></li>
</ul>
<li>Me poia etiketa eisagoume ena senario Javascript?</li>
<ul>
<li><input type="radio" name="java" value="a">&lt;form&gt;&lt;/form&gt;</input></li>
<li><input type="radio" name="java" value="b"> &lt;script&gt;&lt;/script&gt;</input></li>
<li><input type="radio" name="java" value="c">&lt;javascript&gt;&ltjavascript&gt;</input></li>
</ul>
<li>Me poia entoli orizoume mia sinartisi?</li>
<ul>
<li><input type="radio" name="function" value="a">Script</input></li>
<li><input type="radio" name="function" value="b">Program</input></li>
<li><input type="radio" name="function" value="c">Function</input></li>
</ul>
</ol>
<button onClick="reset()">Clear Answers</button>
</form>

<input type="button" value="Get Score" name="score" onClick="getScore()"/>
<p>Score=<input type="text" name="sc"/></p>

<form method="POST">
<script>
var wrong=0
for (e=0;e<=2;e++)
document.result[e].value=""

var results=document.cookie.split(";")
for (n=0;n<=results.length-1;n++){
if (results[n].charAt(1)=='sc')
parse=n

}

var incorrect=results[parse].split("=")
incorrect=incorrect[1].split("/")
if (incorrect[incorrect.length-1]=='b')
incorrect=""
document.result[0].value=totalquestions-incorrect.length+" out of "+totalquestions
document.result[2].value=(totalquestions-incorrect.length)/totalquestions*100+"%"
for (temp=0;temp<incorrect.length;temp++)
document.result[1].value+=incorrect[temp]+", "


</script>
</form>

</div>


</td>
</tr>
<tr>
<td>
</td>
<td>
</br>
<p><iframe width="700" height="400" src="http://www.youtube.com/embed/-HO27zsX3Bw?feature=player_embedded" frameborder="0" allowfullscreen></iframe></p>
</td>
</tr>
<tr>
<td class="footer"><th colspan="2">Ioanna Michael Copyrights 2012</th></td>
</tr>
</table>
</div>
</body>
</html>

Philip M
09-30-2012, 11:20 AM
Have you tried using your error console?

Apart from a brace { } every Javascript statement should be followed by a semi-colon (;). It is quite possible to disregard this advice, but if you do one day it will rise up and bite you in the undercarriage.

joanmicham
09-30-2012, 03:19 PM
Even with this ";" the coding doesn't work can you help me?

Philip M
09-30-2012, 05:23 PM
Suggest you fix the errors in your markup first. See http://validator.w3.org/

33 errors, 12 warnings :(

Quizzes have been covered very many times in this forum. Try using the search feature.

The problem with any quiz such as this where the scoring is done via JavaScript is that it is as secure as tissue paper. Anybody who has the least understanding of Javascript will do View Source and peek at the HTML and JS code and know the answers.

joanmicham
10-01-2012, 01:07 AM
Can you please give me an example or explain me further? I know how to make view source but i have only knowledge on html and no javascript and i can't really understand how it works

jmrker
10-01-2012, 04:34 AM
Can you please give me an example or explain me further? I know how to make view source but i have only knowledge on html and no javascript and i can't really understand how it works

Start here...
http://www.google.com/search?q=site%3Awww.codingforums.com+JavaScript+quiz&qfront=JavaScript+quiz&se=on

Philip M
10-01-2012, 08:51 AM
i have only knowledge on html and no javascript and i can't really understand how it works

So how come that you have an assignment involving Javascript?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum