PDA

Click to See Complete Forum and Search --> : Javascript multiple choice quiz, erroring at the "whats the score" button



cov_dan
01-06-2007, 12:56 PM
I cant figure out for love nor money what is wrong with my code. I fill out the answers and then click on the button to find out the score and nothing happens.

Please help.

Thanks,

Dan

You can find the page at http://www.seafordcrownfc.co.uk/footyikes/footylikes1workingprogress.htm

My javascript is as follows:


<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var ans = new Array;
var done = new Array;
var yourAns = new Array;
var explainAnswer = new Array;

var score = 0;
ans[1] = "a";
ans[2] = "b";
ans[3] = "a";
ans[4] = "c";
ans[5] = "d";
ans[6] = "d";
ans[7] = "d";
ans[8] = "a";
ans[9] = "c";
ans[10] = "b";
ans[11] = "d";
ans[12] = "b";
ans[13] = "a";
ans[14] = "d";
ans[15] = "c";
ans[16] = "c";
ans[17] = "b";
ans[18] = "a";
ans[19] = "d";
ans[20] = "b";
ans[21] = "a";
ans[22] = "b";
ans[23] = "a";
ans[24] = "c";
ans[25] = "d";

explainAnswer[1]="Text";
explainAnswer[2]="Text";
explainAnswer[3]="Text";
explainAnswer[4]="Text";
explainAnswer[5]="Text";
explainAnswer[6]="Text";
explainAnswer[7]="Text";
explainAnswer[8]="Text";
explainAnswer[9]="Text";
explainAnswer[10]="Text";
explainAnswer[11]="Text";
explainAnswer[12]="Text";
explainAnswer[13]="Text";
explainAnswer[14]="Text";
explainAnswer[15]="Text";
explainAnswer[16]="Text";
explainAnswer[17]="Text";
explainAnswer[18]="Text";
explainAnswer[19]="Text";
explainAnswer[20]="Text";
explainAnswer[21]="Text";
explainAnswer[22]="Text";
explainAnswer[23]="Text";
explainAnswer[24]="Text";
explainAnswer[25]="Text";

function Engine(question, answer) {
yourAns[question]=answer;
}

function Score(){
var answerText = "How well do you know your footylikes
?\n------------------------------------\n";
for(i=1;i<=25;i++){
answerText=answerText+"\nQuestion :"+i+"\n";
if(ans[i]!=yourAns[i]){
answerText=answerText+"\nThe correct answer was
"+ans[i]+"\n"+explainAnswer[i]+"\n";
}
else{
answerText=answerText+" \nCorrect! \n";
score++;
}
}

answerText=answerText+"\n\nYour total score is : "+score+"\n";

//now score the user
answerText=answerText+"\nComment : ";
if(score<=0){
answerText=answerText+"Hmmmm, you're either Jason Hibbert or just plain
stupid.";
}
if(score>=1 && score <=9){
answerText=answerText+"Hmmmm...not even double figures. Rubbish.";
}
if(score>=10 && score <=19){
answerText=answerText+"Not bad, room for improvement though.";
}
if(score>=20 && score <=24){
answerText=answerText+"Ooooh, close but no cigar. Well done.";
}
if(score>25){
answerText=answerText+"Perfect score. Impressive for such a loser like
yourself";
}

alert(answerText);

}
// End -->
</script>

cov_dan
01-06-2007, 01:28 PM
Oops ... and here's the HTML

And here's the HTML


<DIV ALIGN="CENTER">
<FORM>
<div align="left"><b><font size="2" face="Gill Sans MT, Verdana, Times New Roman">1.
Eirc Pollard - the wheeler dealer off Emmerdale.</font></b><font size="2" face="Gill Sans MT, Verdana, Times New Roman"><br>
<input type=radio name="q1" value="a" onClick="Engine(1, this.value)">
a) Chambers<br>
<input type=radio name="q1" value="b" onClick="Engine(1, this.value)">
b) Webby<br>
<input type=radio name="q1" value="c" onClick="Engine(1, this.value)">
c) Lord Holmes<br>
<input type=radio name="q1" value="d" onClick="Engine(1, this.value)">
d) Leroy</font> </div>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>2.
Roger Milla - corner post dancing legend.</b><br>
<input type=radio name="q2" value="a" onClick="Engine(2, this.value)">
a) Darts<br>
<input type=radio name="q2" value="b" onClick="Engine(2, this.value)">
b) Lammin<br>
<input type=radio name="q2" value="c" onClick="Engine(2, this.value)">
c) Liam<br>
<input type=radio name="q2" value="d" onClick="Engine(2, this.value)">
d) Rob</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>3.
Sharon Osbourne - X Factor judge and Mrs Ozzy Osbourne.</b><br>
<input type=radio name="q3" value="a" onClick="Engine(3, this.value)">
a) Gumbo<br>
<input type=radio name="q3" value="b" onClick="Engine(3, this.value)">
b) Darts<br>
<input type=radio name="q3" value="c" onClick="Engine(3, this.value)">
c) Studsy<br>
<input type=radio name="q3" value="d" onClick="Engine(3, this.value)">
d) Knels (Ads)</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>4.
Squeek - Star of Matt Stone and Trey Parker film 'Baseketball'. </b><br>
<input type=radio name="q4" value="a" onClick="Engine(4, this.value)">
a) Russ<br>
<input type=radio name="q4" value="b" onClick="Engine(4, this.value)">
b) Dids<br>
<input type=radio name="q4" value="c" onClick="Engine(4, this.value)">
c) Viggers<br>
<input type=radio name="q4" value="d" onClick="Engine(4, this.value)">
d) Scotty D</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>5.
Stingray - Neighbours' resident ruffian.</b><br>
<input type=radio name="q5" value="a" onClick="Engine(5, this.value)">
a) Darts<br>
<input type=radio name="q5" value="b" onClick="Engine(5, this.value)">
b) Puddick<br>
<input type=radio name="q5" value="c" onClick="Engine(5, this.value)">
c) Scouse<br>
<input type=radio name="q5" value="d" onClick="Engine(5, this.value)">
d) Tom Mason</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>6.
Peter Crouch - Liverpool and England goalscoring sensation.</b><br>
<input type=radio name="q6" value="a" onClick="Engine(6, this.value)">
a) Dids<br>
<input type=radio name="q6" value="b" onClick="Engine(6, this.value)">
b) Hund<br>
<input type=radio name="q6" value="c" onClick="Engine(6, this.value)">
c) Pete<br>
<input type=radio name="q6" value="d" onClick="Engine(6, this.value)">
d) Cally</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>7.
Peter Andre - Mr Insania and current 'other half' of Jordan.</b><br>
<input type=radio name="q7" value="a" onClick="Engine(7, this.value)">
a) Viggers<br>
<input type=radio name="q7" value="b" onClick="Engine(7, this.value)">
b) Ruggy<br>
<input type=radio name="q7" value="c" onClick="Engine(7, this.value)">
c) Scotty D<br>
<input type=radio name="q7" value="d" onClick="Engine(7, this.value)">
d) Liam</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>8.
A Bag of Cement - vital for building or doing driveways.</b><br>
<input type=radio name="q8" value="a" onClick="Engine(8, this.value)">
a) Dunc<br>
<input type=radio name="q8" value="b" onClick="Engine(8, this.value)">
b) Gumb<br>
<input type=radio name="q8" value="c" onClick="Engine(8, this.value)">
c) Hibbert<br>
<input type=radio name="q8" value="d" onClick="Engine(8, this.value)">
d) Knels (Ads)</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>9.
Frodo - Little hobbit with overgrown hair.</b><br>
<input type=radio name="q9" value="a" onClick="Engine(9, this.value)">
a) Pete<br>
<input type=radio name="q9" value="b" onClick="Engine(9, this.value)">
b) Leroy<br>
<input type=radio name="q9" value="c" onClick="Engine(9, this.value)">
c) Russ<br>
<input type=radio name="q9" value="d" onClick="Engine(9, this.value)">
d) Viggers</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>10.
Buzz Lightyear - To infinty and beyond!</b><br>
<input type=radio name="q10" value="a" onClick="Engine(10, this.value)">
a) Dids<br>
<input type=radio name="q10" value="b" onClick="Engine(10, this.value)">
b) Roy<br>
<input type=radio name="q10" value="c" onClick="Engine(10, this.value)">
c) Dunc<br>
<input type=radio name="q10" value="d" onClick="Engine(10, this.value)">
d) Lord Holmes</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>11.
Mark Noon - Failed Coventry footballer now plying his trade at Nuneaton
borough</b><br>
<input type=radio name="q11" value="a" onClick="Engine(11, this.value)">
a) Ric*<br>
<input type=radio name="q11" value="b" onClick="Engine(11, this.value)">
b) Webby<br>
<input type=radio name="q11" value="c" onClick="Engine(11, this.value)">
c) Cally<br>
<input type=radio name="q11" value="d" onClick="Engine(11, this.value)">
d) 'H'</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>12.
A really really really old man - Older than time itself.</b><br>
<input type=radio name="q12" value="a" onClick="Engine(12, this.value)">
a) Any need for this option ?<br>
<input type=radio name="q12" value="b" onClick="Engine(12, this.value)">
b) Super Ruggy Rilstone<br>
<input type=radio name="q12" value="c" onClick="Engine(12, this.value)">
c) Any need for this option ?<br>
<input type=radio name="q12" value="d" onClick="Engine(12, this.value)">
d) Any need for this option ?</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>13.
Kal Penn - Asian actor in 'Van Wilder' and 'Harold & Kumar get the Munchies'.</b><br>
<input type=radio name="q13" value="a" onClick="Engine(13, this.value)">
a) Darts<br>
<input type=radio name="q13" value="b" onClick="Engine(13, this.value)">
b) Liam<br>
<input type=radio name="q13" value="c" onClick="Engine(13, this.value)">
c) Studsy<br>
<input type=radio name="q13" value="d" onClick="Engine(13, this.value)">
d) Nathan</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>14.
Elton John - Homosexual pop god.</b><br>
<input type=radio name="q14" value="a" onClick="Engine(14, this.value)">
a) Lord Holmes<br>
<input type=radio name="q14" value="b" onClick="Engine(14, this.value)">
b) Phil<br>
<input type=radio name="q14" value="c" onClick="Engine(14, this.value)">
c) Glen<br>
<input type=radio name="q14" value="d" onClick="Engine(14, this.value)">
d) Beachy</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>15.
Joe from Hollyoaks - Died in the pub along with the most famous people
ever to come out of Seaford</b><br>
<input type=radio name="q15" value="a" onClick="Engine(15, this.value)">
a) Leroy<br>
<input type=radio name="q15" value="b" onClick="Engine(15, this.value)">
b) Ric*<br>
<input type=radio name="q15" value="c" onClick="Engine(15, this.value)">
c) Knels (Ads)<br>
<input type=radio name="q15" value="d" onClick="Engine(15, this.value)">
d) Pete</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>16.
Alan Pardew - Former West Ham and current Charlton manager. I wish he'd
of punched Wenger that time.</b><br>
<input type=radio name="q16" value="a" onClick="Engine(16, this.value)">
a) Dids<br>
<input type=radio name="q16" value="b" onClick="Engine(16, this.value)">
b) 'H'<br>
<input type=radio name="q16" value="c" onClick="Engine(16, this.value)">
c) Studsy<br>
<input type=radio name="q16" value="d" onClick="Engine(16, this.value)">
d) Cally</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>17.
Laura Davies - Lady golfer who also resembles Stephen Lee, the snooker
player.</b><br>
<input type=radio name="q17" value="a" onClick="Engine(17, this.value)">
a) Gumbo<br>
<input type=radio name="q17" value="b" onClick="Engine(17, this.value)">
b) Tel<br>
<input type=radio name="q17" value="c" onClick="Engine(17, this.value)">
c) Webby<br>
<input type=radio name="q17" value="d" onClick="Engine(17, this.value)">
d) Dunc</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>18.
Maggot from GLC - Celeb Big Bro contestant in 2006 and Welsh rapping
extraordinaire.</b><br>
<input type=radio name="q18" value="a" onClick="Engine(18, this.value)">
a) Hund<br>
<input type=radio name="q18" value="b" onClick="Engine(18, this.value)">
b) Cally<br>
<input type=radio name="q18" value="c" onClick="Engine(18, this.value)">
c) Nathan<br>
<input type=radio name="q18" value="d" onClick="Engine(18, this.value)">
d) Knels (Ads)</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>19.
PacMan - Virtual reality round thing which eats a shed load.</b><br>
<input type=radio name="q19" value="a" onClick="Engine(19, this.value)">
a) Gumbo<br>
<input type=radio name="q19" value="b" onClick="Engine(19, this.value)">
b) Darts<br>
<input type=radio name="q19" value="c" onClick="Engine(19, this.value)">
c) Steve Greenwood<br>
<input type=radio name="q19" value="d" onClick="Engine(19, this.value)">
d) Webby</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>20.
Keither Sutherland - 24 and phone booth star.</b><br>
<input type=radio name="q20" value="a" onClick="Engine(20, this.value)">
a) Dids<br>
<input type=radio name="q20" value="b" onClick="Engine(20, this.value)">
b) Mark<br>
<input type=radio name="q20" value="c" onClick="Engine(20, this.value)">
c) Razor<br>
<input type=radio name="q20" value="d" onClick="Engine(20, this.value)">
d) Ruggy</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>21.
A Weeble Toy - One of those toys you push over and they bounce back
up.</b><br>
<input type=radio name="q21" value="a" onClick="Engine(21, this.value)">
a) Steve Greenwood<br>
<input type=radio name="q21" value="b" onClick="Engine(21, this.value)">
b) Viggers<br>
<input type=radio name="q21" value="c" onClick="Engine(21, this.value)">
c) Rob<br>
<input type=radio name="q21" value="d" onClick="Engine(21, this.value)">
d) Roy</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>22.
Heath Ledger - Actor in Holmesy's favourite film, Brokeback Mountain.</b>

cov_dan
01-06-2007, 01:28 PM
code continued ...
<br>
<input type=radio name="q22" value="a" onClick="Engine(22, this.value)">
a) Darts<br>
<input type=radio name="q22" value="b" onClick="Engine(22, this.value)">
b) Pete<br>
<input type=radio name="q22" value="c" onClick="Engine(22, this.value)">
c) Leroy<br>
<input type=radio name="q22" value="d" onClick="Engine(22, this.value)">
d) Nathan</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>23.
Dale Winton - Really really annoying Supermarket Sweep presenter.</b><br>
<input type=radio name="q23" value="a" onClick="Engine(23, this.value)">
a) Lord Holmes<br>
<input type=radio name="q23" value="b" onClick="Engine(23, this.value)">
b) Webby<br>
<input type=radio name="q23" value="c" onClick="Engine(23, this.value)">
c) 'H'<br>
<input type=radio name="q23" value="d" onClick="Engine(23, this.value)">
d) Knels (Ads)</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>24.
Peter Kay - Bolton comic if he bleached his hair blonde.</b><br>
<input type=radio name="q24" value="a" onClick="Engine(24, this.value)">
a) Russ<br>
<input type=radio name="q24" value="b" onClick="Engine(24, this.value)">
b) Webby<br>
<input type=radio name="q24" value="c" onClick="Engine(24, this.value)">
c) Hammers<br>
<input type=radio name="q24" value="d" onClick="Engine(24, this.value)">
d) Studsy</font>
<p align="left"> <font size="2" face="Gill Sans MT, Verdana, Times New Roman"><b>25.
The Sloth - Ugly looking feller from the Goonies, who everyone struggles
to understand what he is saying.</b><br>
<input type=radio name="q25" value="a" onClick="Engine(25, this.value)">
a) Hund<br>
<input type=radio name="q25" value="b" onClick="Engine(25, this.value)">
b) Hibbert<br>
<input type=radio name="q25" value="c" onClick="Engine(25, this.value)">
c) Rob<br>
<input type=radio name="q25" value="d" onClick="Engine(25, this.value)">
d) AFC Southport</font>
<p align="left">
<input type=button onClick="Score()" value="Click here for your score!">
</FORM>

aversion
01-06-2007, 02:52 PM
Well, for a start it's choking at this unterminated string:



var answerText = "How well do you know your footylikes
?\n------------------------------------\n";

I assume that's an accidental hard return and should be:



var answerText = "How well do you know your footylikes?\n------------------------------------\n";

catbert303
01-06-2007, 05:35 PM
Yeah, there are a few other unterminated strings in there too, things like

answerText=answerText+"\nThe correct answer was
"+ans[i]+"\n"+explainAnswer[i]+"\n";

and

answerText=answerText+"Hmmmm, you're either Jason Hibbert or just plain
stupid.";

but if you if you fix these up things should work :)

cov_dan
01-08-2007, 08:08 AM
Pardon my ignorance, but what do you mean by 'unterminated' ???

catbert303
01-08-2007, 08:33 AM
You start a string and then don't close the quote marks in the right place.

for example, this:

answerText=answerText+"\nThe correct answer was
"+ans[i]+"\n"+explainAnswer[i]+"\n";

should be written as

answerText=answerText+"\nThe correct answer was "+ans[i]+"\n"+explainAnswer[i]+"\n";

There are several places throughout the script where some extra line breaks have sneaked in to cause these problems. Other than that the script should be OK though.

cov_dan
01-08-2007, 09:46 AM
Thanks ! Much appreciated

aversion
01-08-2007, 10:41 AM
A line of code may wrap onto a new line if the window you're viewing it in is too small but when you use a hard return, ie, press the return key, it's treated as a new line of code, leaving the first line unterminated.

Sometimes when you copy and paste from one text format to another, such as from an email to a web editor, lines can become broken, so it may not have been something you did on purpose but it's always worth looking out for.

I would also recommend you use the web developer tool bar (https://addons.mozilla.org/firefox/60/) with firefox as it offers tools for analysing errors such as this more accurately. More analysis still is offered by the Firebug plugin, but the web developer tool bar is fine for 'casual' developer use.