/    Sign up×
Community /Pin to ProfileBookmark

Having some difficulty making what should be a simple script work

Been racking my brains wondering why this function doesn’t execute properly. I am trying to make a simple quiz, and while the function does verify wether Q1 is correct, it does not proceed from there and mark the other two questions, nor calculate a total. Can anyone help me?

<html>

<head>

<title>Session 10, Exercise 1</title>

<script type=”text/javascript”>
function markQuiz()
{
var correct = 0;
var nw = window.open();

var answer1 = document.getElementById(“Q1”).value;
if (answer1 == “John”)
{
correct++;
nw.document.write (“<p>Answer 1 was correct</p>”);
}
else
nw.document.write (“<p>Answer 1 was wrong</p>”);

var answer2 = document.getElementById(‘Q2’).value;
if (answer2 == Waratah)
{
correct++;
nw.document.write (“<p>Answer 2 was correct</p>”);
}
else
nw.document.write (“<p>Answer 2 was wrong</p>”);

var answer3 = “none”;
if (quiz.Q3[0].checked)
answer3 = quiz.Q3[0].value;
if (quiz.Q3[1].checked)
answer3 = quiz.Q3[1].value;
if (quiz.Q3[2].checked)
answer3 = quiz.Q3[2].value;
if (answer3 == “Canberra”)
{
correct++;
nw.document.write (“<p>Answer 3 was correct</p>”);
}
else
nw.document.write (“<p>Answer 3 was wrong</p>”);

nw.document.write (“<p>Total answers correct = ” + correct + “</p>”);
nw.document.write (“<input type=’button’ value=’back’ onClick=’window.close();’>”);

}

</script>
</head>
<body>
<h1>General Knowledge Quiz</h1>

<form name=”quiz” id=”quiz”>

<h2>Q1</h2>
<p>What is the first name of Australia’s current Prime Minister</p>
<input type=”text” id=”Q1″ name=”Q1″ />

<h2>Q2</h2>
<p>What is the official state flower of NSW?</p>
<select id=”Q2″ name=”Q2″>
<option value=”Carnation” checked=”checked”>Carnation</option>
<option value=”Waratah”>Waratah</option>
<option value=”Azalea”>Azalea</option>
</select>

<h2>Q3</h2>
<p>Please choose the capital of Australia:</p>
<input type=”radio” name=”Q3″ value=”Sydney” checked=”checked”>Sydney</input><br />
<input type=”radio” name=”Q3″ value=”Melbourne”>Melbourne</input><br />
<input type=”radio” name=”Q3″ value=”Canberra”>Canberra</input><br /><br />

<input type=”button” value=”Mark Quiz” onClick=”markQuiz();”>
</form>
</body>
</html>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FatherSharkauthorOct 19.2005 — please disregard above, I have worked it out:

if (answer2 == Waratah)

Should be:

if (answer2 == "Waratah")
×

Success!

Help @FatherShark spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...