Click to See Complete Forum and Search --> : More Quiz Problems
julep821
03-11-2003, 05:09 PM
I'm currently working on a quiz, and while I can get it to display the first set of results, it won't display the other options. Here is my code for that section of the code, if you need more, please let me know. All it will display is the results for if quizScore==0 || quizScore<=2 irregardless of what the score is.
<script language="javascript">
var DisplayResults = "";
function resultDisplay() {
if (quizScore==0 || quizScore<=2) {
DisplayResults = "Hypnosis may not be for you. You may find it hard to enter a trance and may have trouble responding to hypnotic suggestion.";
document.quiz.result.value = DisplayResults;
} else
if (quizScore==3 || quizScore<=7) {
DisplayResults = "You're somewhere in the middle. While you should have no difficulty being hypnotized, you may not be susceptible to every suggestion.";
document.quiz.result.value = DisplayResults;
} else {
DisplayResults = "Not only will you have no problem going under, but you should respond well to most hypnotic suggestions. Who knows? Hypnosis may help you get through your next root canal.";
document.quiz.results.value= DisplayResults;
}
}
</script>
What is quizScore returning? In the code that you've provided, it is not retuning anything... You need to return it inside of the function...
[EDIT] Removed first post, added this one... ;)
julep821
03-12-2003, 01:19 AM
I have quizScore in another script and it does not return the value there. I have removed the function, because with it,it would not display any results at all.
<html>
<head>
<title>Could You Be Hypnotized?</title>
</head>
<script language="JavaScript" type="text/javascript">
function display() {
DispWin = window.open('', 'NewWin',
'toolbar=no,status=no,width=300, height=200')
message = "Your score: <br>" + quizScore + "<br> Your results: <br>" + document.quiz.result.value;
DispWin.document.write(message);
}
</script>
</head>
<body>
<!--body bgcolor="#84C0FF"-->
<LINK REL=stylesheet HREF="http://www.bubbyweb.com/CSS/bubbyweb.css" TYPE="text/css">
<h1>Could you be hypnotized?</h1>
<p>This is taken from the March 2003 issue of Good Housekeeping Magazine. It is found on page 84.</p>
<p>The ability to be hypnotized is kind of like the ability to draw a picture or carry a tune: Some people
have it and some don't. It's estimated that only 5 percent of adults are unable to achieve any kind of hypnotic
state; the rest of us fall somewhere along a range of "hypnotizability." Curious to know if you'd be susceptible?
This quiz will give you an idea.</p>
<script language="javascript">
var quizScore = 0;
function CalculateScore(scoreBox, calcScore) {
if (scoreBox.checked == true) {
quizScore = quizScore + calcScore;
document.quiz.SCORE.value = quizScore;
}
else {
quizScore = quizScore - calcScore;
document.quiz.SCORE.value = quizScore;
}
}
</script>
<form name="quiz">
<p>1. Do you have many vivid memories from your early childhood?<br>
<input type="checkbox" name="qy1" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn1" value="ON" onClick="CalculateScore(this, 0)">No
<p>2. Do you tend to lose yourself in movies, books, or TV shows?<br>
<input type="checkbox" name="qy2" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn2" value="ON" onClick="CalculateScore(this, 0)">No
<p>3. Do you tend to know what people are going to say before they say it?
<br><input type="checkbox" name="qy3" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn3" value="ON" onClick="CalculateScore(this, 0)">No
<p>4. Do powerful visual images ever trigger a physical senstaion in you? For example, do you feel thirsty during
the desert scenes in <i>Lawrence of Arabia</i>?
<br><input type="checkbox" name="qy4" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn4" value="ON" onClick="CalculateScore(this, 0)">No
<p>5. Have you ever zoned out while going somewhere and wondered how you'd gotten there?
<br><input type="checkbox" name="qy5" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn5" value="ON" onClick="CalculateScore(this, 0)">No
<p>6. Do you sometimes think in images rather than in words?
<br><input type="checkbox" name="qy6" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn6" value="ON" onClick="CalculateScore(this, 0)">No
<p>7. Do you ever sense when someone has entered a room, even before seeing him?
<br><input type="checkbox" name="qy7" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn7" value="ON" onClick="CalculateScore(this, 0)">No
<p>8. Do you like to look at cloud shapes?
<br><input type="checkbox" name="qy8" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn8" value="ON" onClick="CalculateScore(this, 0)">No
<p>9. Do smells evoke powerful memories for you?
<br><input type="checkbox" name="qy9" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn9" value="ON" onClick="CalculateScore(this, 0)">No
<p>10. Have you ever been deeply moved by a sunset?
<br><input type="checkbox" name="qy10" value="ON" onClick="CalculateScore(this, 1)">Yes
<input type="checkbox" name="qn10" value="ON" onClick="CalculateScore(this, 0)">No
<p>Score:
<input name=SCORE size="10" onFocus="window.focus()" onBlur="this.value=quizScore;">
<input type=hidden name=result>
<script language="javascript">
if (quizScore >=0 && quizScore <=2) {
quizResult = "Hypnosis may not be for you. You may find it hard to enter a trance and may have trouble responding to hypnotic suggestion.";
document.quiz.result.value = quizResult;
}
else if (quizScore >=3 && quizScore <=7) {
quizResult = "You're somewhere in the middle. While you should have no difficulty being hypnotized, you may not be susceptible to every suggestion.";
document.quiz.result.value = quizResult;
}
else {
quizResult = "Not only will you have no problem going under, but you should respond well to most hypnotic suggestions. Who knows? Hypnosis may help you get through your next root canal.";
document.quiz.result.value = quizResult;
}
</script>
<p><input type="button" value="Get Results" onClick="display();"></p>
</form>
</body>
</html>
cyberade
03-12-2003, 04:27 AM
As usual, Pyro is right. 'quizScore' is being assigned to 'document.quiz.SCORE.value' so that's what you should be using in the condition.
Funnily enough, after you're post yesterday I was playing around with this very script putting the questions and choices into a table.
I've attached my version as a guide to how the scoring should work only - you can keep you're own formatting. Note though that, wherever possible, javascript should be placed in the <head> of a page.
The file should work if you rename it (to whatever you like) with a .html extension.
HTH
julep821
03-12-2003, 12:47 PM
Just wanted to say thank you! Between you and Pyro I got it to work. I appreciate it a lot!
Why is it that it's preferred to have the script code in the head tag? Just because it's easier and neater to read?
I've seen it done both ways.
Nevermore
03-12-2003, 03:07 PM
Because it is read by the browser first, so if you need to refer to it later, it has definately been recognised already.
And, in the cases that you need it to do something after you pages is done loading, us <body onload="somefunction()">