IF statement for a beginner

O

OrdinarySpore

Hi. I am having a problem with a simple IF statment on this
Javascript. I found this script on scriptsource and want to see if I
can add an IF statment to display a "congradulations" line of text if
the 5 questions on the Quiz are correct. I'm stumped and I think it's
somthing simple but I thought it would work if I added the statement
after the "Check Answer fuction like this ------- IF var score > 5) {
}


Please help if you can. Thanks. I plan to change and minipulate it
if it's worth it.

<html>
<head>
<title>Trivia Quiz v 1.0 by POZ ([email protected])</title>
<style type="text/css">
a,a:active, a:visited {color:blue; text-decoration:none;}
a:hover {color:red; text-decoration:none;}
</style>
<script language="javascript">
/*
Trivia Quiz v 1.0
by POZ
comments, bug reports, suggestions will be welcome at
(e-mail address removed)
may be used freely as long as this credit is maintained.
*/

/*
Easy customizing instructions:

1. Add as many questions as you like according to the structure of the
existing demo questions.
Make sure that every question is properly enveloped with <div
id="questionX" style="display:none">...</div>
where X is the number of the question (numbers must be sequential)
2. Adjust the correctAnswers array to hold the letter of the correct
answer for each question.
3. Enjoy.
*/

// how many seconds available to answer each question
var delay=10;

// an array listing the correct answer for each question in order of
appearance
var correctAnswers=new Array("c","d","b","d","c");

// =====================================================
// do not change script below this line
// =====================================================
var q_num=1;
var timer;
var layer;
var clock=delay;
var score=0;

function show_question(){
if (layer=eval("document.all.question"+q_num)){
layer.style.display="inline";
document.all.timeLeft.innerHTML=clock;
hide_question();
} else {
document.all.timeLeft.innerHTML=0;
document.all.quizScore.innerHTML="You have answered correctly "+score
+" out of "+(q_num-1)+" questions.";
document.all.quizScore.style.display="inline";
}
}

function hide_question(){
if (clock>0) {
document.all.timeLeft.innerHTML=clock;
clock--;
timer=setTimeout("hide_question()",1000);
} else {
clearTimeout(timer);
document.all.answerBoard.innerHTML=" ";
clock=delay;
layer.style.display="none";
q_num++;
show_question();
}
}

function check_answer(answer){
if (correctAnswers[q_num-1]==answer){
score++;
document.all.answerBoard.innerHTML="<i><b>Correct!</b></i>";
} else {
document.all.answerBoard.innerHTML="<i><b>Wrong!</b></i>";
}
clock=0;
clearTimeout(timer);
timer=setTimeout("hide_question()",1500);
}

window.onload=show_question;
</script>
</head>

<body>

Time left: <span id="timeLeft"></span> seconds<br>
<br>
<div id="answerBoard"> </div>
<br>

<div id="question1" style="display:none">
<b>1. Who was the first man on the moon?</b><br>
<a href="javascript:void(0)" onclick="check_answer('a')">a) Captain
Kirk</a><br>
<a href="javascript:void(0)" onclick="check_answer('b')">b) Tom Hanks</
a><br>
<a href="javascript:void(0)" onclick="check_answer('c')">c) Neil
Armstrong</a><br>
<a href="javascript:void(0)" onclick="check_answer('d')">d) Bugs
Bunny</a><br>
</div>

<div id="question2" style="display:none">
<b>2. What is the largest land mammal on earth?</b><br>
<a href="javascript:void(0)" onclick="check_answer('a')">a) John
Goodman</a><br>
<a href="javascript:void(0)" onclick="check_answer('b')">b) Yo Mama</
a><br>
<a href="javascript:void(0)" onclick="check_answer('c')">c) Humpback
Whale</a><br>
<a href="javascript:void(0)" onclick="check_answer('d')">d) African
Elephant</a><br>
</div>

<div id="question3" style="display:none">
<b>3. Why?</b><br>
<a href="javascript:void(0)" onclick="check_answer('a')">a) Because</
a><br>
<a href="javascript:void(0)" onclick="check_answer('b')">b) Why not?</
a><br>
<a href="javascript:void(0)" onclick="check_answer('c')">c) Who?</
a><br>
<a href="javascript:void(0)" onclick="check_answer('d')">d) He did it!
</a><br>
</div>

<div id="question4" style="display:none">
<b>4. Which of the following is the longest?</b><br>
<a href="javascript:void(0)" onclick="check_answer('a')">a) one foot</
a><br>
<a href="javascript:void(0)" onclick="check_answer('b')">b) one
kilometer</a><br>
<a href="javascript:void(0)" onclick="check_answer('c')">c) one mile</
a><br>
<a href="javascript:void(0)" onclick="check_answer('d')">d) 753 light
years</a><br>
</div>

<div id="question5" style="display:none">
<b>5. Which of the following actors is least likely to ever win an
oscar?</b><br>
<a href="javascript:void(0)" onclick="check_answer('a')">a) Bruce
Willis</a><br>
<a href="javascript:void(0)" onclick="check_answer('b')">b) James
Gandolfini</a><br>
<a href="javascript:void(0)" onclick="check_answer('c')">c) Steven
Seagal</a><br>
<a href="javascript:void(0)" onclick="check_answer('d')">d) Mr. T</
a><br>
</div>

<div id="quizScore" style="display:none">
</div>

</body>
</html>
 
L

-Lost

Hi. I am having a problem with a simple IF statment on this
Javascript. I found this script on scriptsource and want to see if I
can add an IF statment to display a "congradulations" line of text if
the 5 questions on the Quiz are correct. I'm stumped and I think it's
somthing simple but I thought it would work if I added the statement
after the "Check Answer fuction like this ------- IF var score > 5) {
}

<snip script contents>

Sure, you could use:

if (score === 5) { alert('Yay, perfect score!'); }

.... in the bottom of the check answer function.

You could have found that from any search on Google though!

I would also change it to adhere to standard DOM methods and properties. For example,
docment.all is a no-no. You should always feature detect rather than browser detect which
insures that you use DOM methods.

-Lost
 
R

Randy Webb

-Lost said the following on 1/30/2007 12:08 AM:
<snip script contents>

Sure, you could use:

if (score === 5) { alert('Yay, perfect score!'); }

Be careful with === though unless you understand what is going to happen
if you feed it "5" and want a true result. And, in the OP, IF != if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top