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]
3 1669
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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>
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |