469,631 Members | 1,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Simple Dinosaur Quiz

Hello,

I am pretty new to JavaScript. I was trying one of the examples in my Text Book JavaScript Third Edition and for some reason I can't seem to get the windows alert popups to say if the answer is correct or incorrect. Below is the code that the book has me using. Can someone please tell me where I have gone wrong and how I can fix it.

Thank you so much.

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/dtd/xhtml1-strict.dtd">
<html>
<head>
<title>Dinosaur Quiz</title>
<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
A response of "Correct Answer" appears if the user provides the correct answer.
A response of "Incorrect Answer" appears if the user provides the incorrect answer.
function scoreQuestion1 (b) {
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Correct Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion2 (b){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Correct Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion3 (c){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Correct Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion4 (d){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Correct Answer");
}
function scoreQuestion5 (a){
if (answer == "a")
window.alert("Correct Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body>
<h1>Dinosaur Quiz</h1>
<form action="">
<p><b>1. Which dinosaur was the heaviest?</b></p>
<p><input type="radio" name="question1" value="a"
onclick="scoreQuestion1"('a') />Tyrannosaurus<br />
<input type="radio" name="question1" value="b"
onclick="scoreQuestion1"('b') />Brachiosaurus<br />
<input type="radio" name="question1" value="c"
onclick="scoreQuestion1"('c') />Brontosaurus<br />
<input type="radio" name="question1" value="d"
onclick="scoreQuestion1"('d') />Diplodocus</p>
<p><b>2. When did dinosaurs become extinct?</b></p>
<p><input type="radio" name="question2" value="a"
onclick="scoreQuestion2"('a') />1 billion years ago<br />
<input type="radio" name="question2" value="b"
onclick="scoreQuestion2"('b') />65 million years ago<br />
<input type="radio" name="question2" value="c"
onclick="scoreQuestion2"('c') />10 million years ago<br />
<input type="radio" name="question2" value="d"
onclick="scoreQuestion2"('d') />15,000 years ago</p>
<p><b>3. Which of these is not a dinosaur?</b></p>
<p><input type="radio" name="question3" value="a"
onclick="scoreQuestion3"('a') />Megalosaurus<br />
<input type="radio" name="question3" value="b"
onclick="scoreQuestion3"('b') />Apatosaurus<br />
<input type="radio" name="question3" value="c"
onclick="scoreQuestion3"('c') />Pterodactyl<br />
<input type="radio" name="question3" value="d"
onclick="scoreQuestion3"('d') />Triceratops</p>
<p><b>4. In which period did the Diplodocus live?</b></p>
<p><input type="radio" name="question4" value="a"
onclick="scoreQuestion4"('a') />Early Cretaceous<br />
<input type="radio" name="question4" value="b"
onclick="scoreQuestion4"('b') />Early Triassic<br />
<input type="radio" name="question4" value="c"
onclick="scoreQuestion4"('c') />Late Triassic<br />
<input type="radio" name="question4" value="d"
onclick="scoreQuestion4"('d') />Late Jurassic</p>
<p><b>5. Which of the following dinosaurs was a carnivore?</b></p>
<p><input type="radio" name="question5" value="a"
onclick="scoreQuestion5"('a') />Tryannosaurus<br />
<input type="radio" name="question5" value="b"
onclick="scoreQuestion5"('b') />Triceratops<br />
<input type="radio" name="question5" value="c"
onclick="scoreQuestion5"('c') />Stegosaurus<br />
<input type="radio" name="question5" value="d"
onclick="scoreQuestion5"('d') />Brontosaurus</p>
</form>

</body>
</html>[/HTML]
Jan 24 '07 #1
3 1537
r035198x
13,262 8TB
Hello,

I am pretty new to JavaScript. I was trying one of the examples in my Text Book JavaScript Third Edition and for some reason I can't seem to get the windows alert popups to say if the answer is correct or incorrect. Below is the code that the book has me using. Can someone please tell me where I have gone wrong and how I can fix it.

Thank you so much.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/dtd/xhtml1-strict.dtd">
<html>
<head>
<title>Dinosaur Quiz</title>
<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
A response of "Correct Answer" appears if the user provides the correct answer.
A response of "Incorrect Answer" appears if the user provides the incorrect answer.
function scoreQuestion1 (b) {
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Correct Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion2 (b){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Correct Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion3 (c){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Correct Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion4 (d){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Correct Answer");
}
function scoreQuestion5 (a){
if (answer == "a")
window.alert("Correct Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body>
<h1>Dinosaur Quiz</h1>
<form action="">
<p><b>1. Which dinosaur was the heaviest?</b></p>
<p><input type="radio" name="question1" value="a"
onclick="scoreQuestion1"('a') />Tyrannosaurus<br />
<input type="radio" name="question1" value="b"
onclick="scoreQuestion1"('b') />Brachiosaurus<br />
<input type="radio" name="question1" value="c"
onclick="scoreQuestion1"('c') />Brontosaurus<br />
<input type="radio" name="question1" value="d"
onclick="scoreQuestion1"('d') />Diplodocus</p>
<p><b>2. When did dinosaurs become extinct?</b></p>
<p><input type="radio" name="question2" value="a"
onclick="scoreQuestion2"('a') />1 billion years ago<br />
<input type="radio" name="question2" value="b"
onclick="scoreQuestion2"('b') />65 million years ago<br />
<input type="radio" name="question2" value="c"
onclick="scoreQuestion2"('c') />10 million years ago<br />
<input type="radio" name="question2" value="d"
onclick="scoreQuestion2"('d') />15,000 years ago</p>
<p><b>3. Which of these is not a dinosaur?</b></p>
<p><input type="radio" name="question3" value="a"
onclick="scoreQuestion3"('a') />Megalosaurus<br />
<input type="radio" name="question3" value="b"
onclick="scoreQuestion3"('b') />Apatosaurus<br />
<input type="radio" name="question3" value="c"
onclick="scoreQuestion3"('c') />Pterodactyl<br />
<input type="radio" name="question3" value="d"
onclick="scoreQuestion3"('d') />Triceratops</p>
<p><b>4. In which period did the Diplodocus live?</b></p>
<p><input type="radio" name="question4" value="a"
onclick="scoreQuestion4"('a') />Early Cretaceous<br />
<input type="radio" name="question4" value="b"
onclick="scoreQuestion4"('b') />Early Triassic<br />
<input type="radio" name="question4" value="c"
onclick="scoreQuestion4"('c') />Late Triassic<br />
<input type="radio" name="question4" value="d"
onclick="scoreQuestion4"('d') />Late Jurassic</p>
<p><b>5. Which of the following dinosaurs was a carnivore?</b></p>
<p><input type="radio" name="question5" value="a"
onclick="scoreQuestion5"('a') />Tryannosaurus<br />
<input type="radio" name="question5" value="b"
onclick="scoreQuestion5"('b') />Triceratops<br />
<input type="radio" name="question5" value="c"
onclick="scoreQuestion5"('c') />Stegosaurus<br />
<input type="radio" name="question5" value="d"
onclick="scoreQuestion5"('d') />Brontosaurus</p>
</form>

</body>
</html>
Hi Java forum moderators don't like to see Javascript questions in their forum, so I'll move this to the Javascript forum.

Javascript forum moderators don't like to see code posted without code tags so I'll add those for you.
Jan 25 '07 #2
r035198x
13,262 8TB
Hello,

I am pretty new to JavaScript. I was trying one of the examples in my Text Book JavaScript Third Edition and for some reason I can't seem to get the windows alert popups to say if the answer is correct or incorrect. Below is the code that the book has me using. Can someone please tell me where I have gone wrong and how I can fix it.

Thank you so much.

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/dtd/xhtml1-strict.dtd">
<html>
<head>
<title>Dinosaur Quiz</title>
<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
A response of "Correct Answer" appears if the user provides the correct answer.
A response of "Incorrect Answer" appears if the user provides the incorrect answer.
function scoreQuestion1 (b) {
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Correct Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion2 (b){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Correct Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion3 (c){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Correct Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion4 (d){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Correct Answer");
}
function scoreQuestion5 (a){
if (answer == "a")
window.alert("Correct Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body>
<h1>Dinosaur Quiz</h1>
<form action="">
<p><b>1. Which dinosaur was the heaviest?</b></p>
<p><input type="radio" name="question1" value="a"
onclick="scoreQuestion1"('a') />Tyrannosaurus<br />
<input type="radio" name="question1" value="b"
onclick="scoreQuestion1"('b') />Brachiosaurus<br />
<input type="radio" name="question1" value="c"
onclick="scoreQuestion1"('c') />Brontosaurus<br />
<input type="radio" name="question1" value="d"
onclick="scoreQuestion1"('d') />Diplodocus</p>
<p><b>2. When did dinosaurs become extinct?</b></p>
<p><input type="radio" name="question2" value="a"
onclick="scoreQuestion2"('a') />1 billion years ago<br />
<input type="radio" name="question2" value="b"
onclick="scoreQuestion2"('b') />65 million years ago<br />
<input type="radio" name="question2" value="c"
onclick="scoreQuestion2"('c') />10 million years ago<br />
<input type="radio" name="question2" value="d"
onclick="scoreQuestion2"('d') />15,000 years ago</p>
<p><b>3. Which of these is not a dinosaur?</b></p>
<p><input type="radio" name="question3" value="a"
onclick="scoreQuestion3"('a') />Megalosaurus<br />
<input type="radio" name="question3" value="b"
onclick="scoreQuestion3"('b') />Apatosaurus<br />
<input type="radio" name="question3" value="c"
onclick="scoreQuestion3"('c') />Pterodactyl<br />
<input type="radio" name="question3" value="d"
onclick="scoreQuestion3"('d') />Triceratops</p>
<p><b>4. In which period did the Diplodocus live?</b></p>
<p><input type="radio" name="question4" value="a"
onclick="scoreQuestion4"('a') />Early Cretaceous<br />
<input type="radio" name="question4" value="b"
onclick="scoreQuestion4"('b') />Early Triassic<br />
<input type="radio" name="question4" value="c"
onclick="scoreQuestion4"('c') />Late Triassic<br />
<input type="radio" name="question4" value="d"
onclick="scoreQuestion4"('d') />Late Jurassic</p>
<p><b>5. Which of the following dinosaurs was a carnivore?</b></p>
<p><input type="radio" name="question5" value="a"
onclick="scoreQuestion5"('a') />Tryannosaurus<br />
<input type="radio" name="question5" value="b"
onclick="scoreQuestion5"('b') />Triceratops<br />
<input type="radio" name="question5" value="c"
onclick="scoreQuestion5"('c') />Stegosaurus<br />
<input type="radio" name="question5" value="d"
onclick="scoreQuestion5"('d') />Brontosaurus</p>
</form>

</body>
</html>[/HTML]
In your Javascript you are comparing answer with values a,b,c, d but you have not assigned answer to anything. You should first get the value that was selected into answer.
Jan 25 '07 #3
acoder
16,027 Expert Mod 8TB
You have probably misunderstood the code and are programming the answer checking code incorrectly.

For each function, e.g. scoreQuestion1, you have set the correct answer letter as the argument, e.g. in the case of scoreQuestion1, b. That is fine as long as you use that value to check for the correct answer. Since you are using the answer variable, change each of those letters to answer.
Jan 25 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Sketcher | last post: by
1 post views Thread by number1.email | last post: by
4 posts views Thread by DAL | last post: by
reply views Thread by philip | last post: by
3 posts views Thread by Raqueeb Hassan | last post: by
nomad
5 posts views Thread by nomad | last post: by
3 posts views Thread by empiresolutions | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.