473,804 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Dinosaur Quiz

1 New Member
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>Dinosa ur 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("I ncorrect Answer");
if (answer == "b")
window.alert("C orrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("I ncorrect Answer");
}
function scoreQuestion2 (b){
if (answer == "a")
window.alert("I ncorrect Answer");
if (answer == "b")
window.alert("C orrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("I ncorrect Answer");
}
function scoreQuestion3 (c){
if (answer == "a")
window.alert("I ncorrect Answer");
if (answer == "b")
window.alert("I ncorrect Answer");
if (answer == "c")
window.alert("C orrect Answer");
if (answer == "d")
window.alert("I ncorrect Answer");
}
function scoreQuestion4 (d){
if (answer == "a")
window.alert("I ncorrect Answer");
if (answer == "b")
window.alert("I ncorrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("C orrect Answer");
}
function scoreQuestion5 (a){
if (answer == "a")
window.alert("C orrect Answer");
if (answer == "b")
window.alert("I ncorrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("I ncorrect 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="scoreQ uestion1"('a') />Tyrannosaurus< br />
<input type="radio" name="question1 " value="b"
onclick="scoreQ uestion1"('b') />Brachiosaurus< br />
<input type="radio" name="question1 " value="c"
onclick="scoreQ uestion1"('c') />Brontosaurus<b r />
<input type="radio" name="question1 " value="d"
onclick="scoreQ uestion1"('d') />Diplodocus</p>
<p><b>2. When did dinosaurs become extinct?</b></p>
<p><input type="radio" name="question2 " value="a"
onclick="scoreQ uestion2"('a') />1 billion years ago<br />
<input type="radio" name="question2 " value="b"
onclick="scoreQ uestion2"('b') />65 million years ago<br />
<input type="radio" name="question2 " value="c"
onclick="scoreQ uestion2"('c') />10 million years ago<br />
<input type="radio" name="question2 " value="d"
onclick="scoreQ uestion2"('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="scoreQ uestion3"('a') />Megalosaurus<b r />
<input type="radio" name="question3 " value="b"
onclick="scoreQ uestion3"('b') />Apatosaurus< br />
<input type="radio" name="question3 " value="c"
onclick="scoreQ uestion3"('c') />Pterodactyl< br />
<input type="radio" name="question3 " value="d"
onclick="scoreQ uestion3"('d') />Triceratops</p>
<p><b>4. In which period did the Diplodocus live?</b></p>
<p><input type="radio" name="question4 " value="a"
onclick="scoreQ uestion4"('a') />Early Cretaceous<br />
<input type="radio" name="question4 " value="b"
onclick="scoreQ uestion4"('b') />Early Triassic<br />
<input type="radio" name="question4 " value="c"
onclick="scoreQ uestion4"('c') />Late Triassic<br />
<input type="radio" name="question4 " value="d"
onclick="scoreQ uestion4"('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="scoreQ uestion5"('a') />Tryannosaurus< br />
<input type="radio" name="question5 " value="b"
onclick="scoreQ uestion5"('b') />Triceratops< br />
<input type="radio" name="question5 " value="c"
onclick="scoreQ uestion5"('c') />Stegosaurus< br />
<input type="radio" name="question5 " value="d"
onclick="scoreQ uestion5"('d') />Brontosaurus </p>
</form>

</body>
</html>[/HTML]
Jan 24 '07 #1
3 1690
r035198x
13,262 MVP
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>Dinosa ur 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("I ncorrect Answer");
if (answer == "b")
window.alert("C orrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("I ncorrect Answer");
}
function scoreQuestion2 (b){
if (answer == "a")
window.alert("I ncorrect Answer");
if (answer == "b")
window.alert("C orrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("I ncorrect Answer");
}
function scoreQuestion3 (c){
if (answer == "a")
window.alert("I ncorrect Answer");
if (answer == "b")
window.alert("I ncorrect Answer");
if (answer == "c")
window.alert("C orrect Answer");
if (answer == "d")
window.alert("I ncorrect Answer");
}
function scoreQuestion4 (d){
if (answer == "a")
window.alert("I ncorrect Answer");
if (answer == "b")
window.alert("I ncorrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("C orrect Answer");
}
function scoreQuestion5 (a){
if (answer == "a")
window.alert("C orrect Answer");
if (answer == "b")
window.alert("I ncorrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("I ncorrect 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="scoreQ uestion1"('a') />Tyrannosaurus< br />
<input type="radio" name="question1 " value="b"
onclick="scoreQ uestion1"('b') />Brachiosaurus< br />
<input type="radio" name="question1 " value="c"
onclick="scoreQ uestion1"('c') />Brontosaurus<b r />
<input type="radio" name="question1 " value="d"
onclick="scoreQ uestion1"('d') />Diplodocus</p>
<p><b>2. When did dinosaurs become extinct?</b></p>
<p><input type="radio" name="question2 " value="a"
onclick="scoreQ uestion2"('a') />1 billion years ago<br />
<input type="radio" name="question2 " value="b"
onclick="scoreQ uestion2"('b') />65 million years ago<br />
<input type="radio" name="question2 " value="c"
onclick="scoreQ uestion2"('c') />10 million years ago<br />
<input type="radio" name="question2 " value="d"
onclick="scoreQ uestion2"('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="scoreQ uestion3"('a') />Megalosaurus<b r />
<input type="radio" name="question3 " value="b"
onclick="scoreQ uestion3"('b') />Apatosaurus< br />
<input type="radio" name="question3 " value="c"
onclick="scoreQ uestion3"('c') />Pterodactyl< br />
<input type="radio" name="question3 " value="d"
onclick="scoreQ uestion3"('d') />Triceratops</p>
<p><b>4. In which period did the Diplodocus live?</b></p>
<p><input type="radio" name="question4 " value="a"
onclick="scoreQ uestion4"('a') />Early Cretaceous<br />
<input type="radio" name="question4 " value="b"
onclick="scoreQ uestion4"('b') />Early Triassic<br />
<input type="radio" name="question4 " value="c"
onclick="scoreQ uestion4"('c') />Late Triassic<br />
<input type="radio" name="question4 " value="d"
onclick="scoreQ uestion4"('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="scoreQ uestion5"('a') />Tryannosaurus< br />
<input type="radio" name="question5 " value="b"
onclick="scoreQ uestion5"('b') />Triceratops< br />
<input type="radio" name="question5 " value="c"
onclick="scoreQ uestion5"('c') />Stegosaurus< br />
<input type="radio" name="question5 " value="d"
onclick="scoreQ uestion5"('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 MVP
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>Dinosa ur 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("I ncorrect Answer");
if (answer == "b")
window.alert("C orrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("I ncorrect Answer");
}
function scoreQuestion2 (b){
if (answer == "a")
window.alert("I ncorrect Answer");
if (answer == "b")
window.alert("C orrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("I ncorrect Answer");
}
function scoreQuestion3 (c){
if (answer == "a")
window.alert("I ncorrect Answer");
if (answer == "b")
window.alert("I ncorrect Answer");
if (answer == "c")
window.alert("C orrect Answer");
if (answer == "d")
window.alert("I ncorrect Answer");
}
function scoreQuestion4 (d){
if (answer == "a")
window.alert("I ncorrect Answer");
if (answer == "b")
window.alert("I ncorrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("C orrect Answer");
}
function scoreQuestion5 (a){
if (answer == "a")
window.alert("C orrect Answer");
if (answer == "b")
window.alert("I ncorrect Answer");
if (answer == "c")
window.alert("I ncorrect Answer");
if (answer == "d")
window.alert("I ncorrect 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="scoreQ uestion1"('a') />Tyrannosaurus< br />
<input type="radio" name="question1 " value="b"
onclick="scoreQ uestion1"('b') />Brachiosaurus< br />
<input type="radio" name="question1 " value="c"
onclick="scoreQ uestion1"('c') />Brontosaurus<b r />
<input type="radio" name="question1 " value="d"
onclick="scoreQ uestion1"('d') />Diplodocus</p>
<p><b>2. When did dinosaurs become extinct?</b></p>
<p><input type="radio" name="question2 " value="a"
onclick="scoreQ uestion2"('a') />1 billion years ago<br />
<input type="radio" name="question2 " value="b"
onclick="scoreQ uestion2"('b') />65 million years ago<br />
<input type="radio" name="question2 " value="c"
onclick="scoreQ uestion2"('c') />10 million years ago<br />
<input type="radio" name="question2 " value="d"
onclick="scoreQ uestion2"('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="scoreQ uestion3"('a') />Megalosaurus<b r />
<input type="radio" name="question3 " value="b"
onclick="scoreQ uestion3"('b') />Apatosaurus< br />
<input type="radio" name="question3 " value="c"
onclick="scoreQ uestion3"('c') />Pterodactyl< br />
<input type="radio" name="question3 " value="d"
onclick="scoreQ uestion3"('d') />Triceratops</p>
<p><b>4. In which period did the Diplodocus live?</b></p>
<p><input type="radio" name="question4 " value="a"
onclick="scoreQ uestion4"('a') />Early Cretaceous<br />
<input type="radio" name="question4 " value="b"
onclick="scoreQ uestion4"('b') />Early Triassic<br />
<input type="radio" name="question4 " value="c"
onclick="scoreQ uestion4"('c') />Late Triassic<br />
<input type="radio" name="question4 " value="d"
onclick="scoreQ uestion4"('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="scoreQ uestion5"('a') />Tryannosaurus< br />
<input type="radio" name="question5 " value="b"
onclick="scoreQ uestion5"('b') />Triceratops< br />
<input type="radio" name="question5 " value="c"
onclick="scoreQ uestion5"('c') />Stegosaurus< br />
<input type="radio" name="question5 " value="d"
onclick="scoreQ uestion5"('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 Recognized Expert Moderator MVP
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

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

Similar topics

2
2655
by: Sketcher | last post by:
Hi, I am trying to create a quiz, Code is as follows: <html> <head> <title>Quiz</title> </head> <BODY> <Center><TABLE cellSpacing=3 cellPadding=0 border=0>
1
1465
by: number1.email | last post by:
Hello, I have a simple Web Page Questionairre in which questions are read from a database, and the user can indicate the correct answer via either a radio input control or a dropdown list. The number of questions that is displayed on the screen can vary...depending on the number of questions that satisfy certain criteria. Does anyone have any sample code, or can show me how I can validate this Web Page in JavaScript so that the user is...
4
7477
by: DAL | last post by:
I want to build my kid a program that cycles through questions (using a label for the question), and lets him choose one of two radio buttons for the right answer. How do I get every set of questions and answers to cycle through until the last question? Also, how can I give him the score after the last question. Thank you in advance. DAL. P.S. As a beginner, I figured I couldn't pass up the chance to learn something new, and to practice...
0
1658
by: philip | last post by:
hello, i am now developing a quiz application for my school using ASP.NET and SQL SERVER 2005, here is a senario: It will have 20 students for taking a quiz in a classroom, they have to answer randomly generated 100 questions in 90 minutes from database, each of those questions may have a picture (approximately 200K), and the picture is stored in the database as image type. i have 2 methods for retrieving 100 questions for each student.
0
4584
NoPeasHear
by: NoPeasHear | last post by:
I don't know what I am doing wrong... I used this tutorial... http://www.permadi.com/tutorial/flashMXQuiz/index.html It works with their quiz.xml file, but when I add an option for multiple correct answers, the results page always gives me a response of 0. What would I change from the tutorial to have multiple correct responses reflect in the results? <?xml version="1.0"?>
3
4410
by: Raqueeb Hassan | last post by:
Hello, I was helping one of my friend's school on setting up a online quiz system. They have the AMP systems to host php+mysql. The quiz script/software should have the following features: a. Excellent results screen (customizable) which can be printed right away. b. 3/5 simultaneously run quizes along with a authentication system. c. Should be able to handle MCQs (multiple choice questions) along with
1
3097
by: korr | last post by:
Hi there, i'm trying to develop a quiz in flash. Searching on the net, I found a quiz in flashkit from sephiroth.it by Alessandro Crugnola. His quiz has a script that puts the questions and the answers in random order. What i'm looking for is to not select the answers by clicking the mouse, but using the keyPress handler. Using the keyPress handler the script selects only the first answer I tried a lot but i didn't find any solution.. 2...
5
2184
nomad
by: nomad | last post by:
Hello Everyone: Just want to ask how easy would it be to build a quiz in Java. I have not use Java for a few months (5). Quiz would need the following: 1. T or F and mulitiple question, possible write the answer 2. Client must register and sign in to take the test/quiz (db is needed) 3. Quiz gives final results of the test: How many they got right, pass or fail 4. DB holds results of the tests. 5. Must be able to take the quiz on-line ie...
3
2224
by: empiresolutions | last post by:
I am building a app that creates quizzes. This is how it goes - - Create Quiz - Provide up to 10 different types of Quiz Results - Give up to 50 Questions - Each Question has up to 10 possible Answers. - Each Answer is assigned a Weighted value.... for each type of Quiz Result. - Weighted values are in the range of -6, 0, +6. - Each Quiz will also apply the same Weight range for if a M/F is taking and what of 6 age groups the taker is...
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9171
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.