/    Sign up×
Community /Pin to ProfileBookmark

a different display of result

Hi all,

I am just beginning to learn javascript…so to this point i no very little….I have a multiplechoice quiz with more than one possible answer for some of the questions when i click submit button it tells me my results by way of an alert box….what i want to happen is when submit button is clicked the questions will stay on the page and the correct answers will be highlighted in any color and it’ll show me if i got them right or wrong….If this is possible any help would be greatly appreciated on how to do it…here is the code for the quiz to date…

[CODE]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>Untitled</title>
<script language=”JavaScript” type=”text/javascript”>
<!–
var questions=new Array();
questions[0]=new Array(1,0,1,0);//first and third answer=correct, others=incorrect
questions[1]=new Array(0,0,0,1);//fourth answer=correct, others=incorrect
questions[2]=new Array(0,0,1,0);//third answer=correct, others=incorrect
function checkAnswers(){
var falseAnswers=new Array();
var answers=new Array();
for(var i=0;i<questions.length;i++){
answers[i]=new Array();
}
var boxArr=new Array();
var checkArrays=new Array();
var count=-1;
var cbox=document.getElementsByTagName(‘input’);
for (var i=0;i<cbox.length;i++){
if(cbox[i].type==’checkbox’){
count++;
boxArr[count]=cbox[i].name;
}
}
for(var i=0;i<boxArr.length;i++){
checkArrays[i]=boxArr[i].split(‘_’);
var quest=checkArrays[i][1];
for(var j=0;j<questions.length;j++){
if(quest==j){
if(document.myquiz[‘question_’+j+’_’+checkArrays[i][2]].checked){
var ins=1;
}
else{
var ins=0;
}
answers[j][checkArrays[i][2]]=(ins);
}
}
}
count=-1;
for(var j=0;j<questions.length;j++){
var answ=answers[j].toString();
var ques=questions[j].toString() ;
if(ques!=answ){
count++;
falseAnswers[count]=’Mistake in question ‘+(j+1)+’n’;
}
}
var mistakes=””;
for(var j=0;j<falseAnswers.length;j++){
mistakes+=falseAnswers[j];
}
if (mistakes==””){
mistakes=”Congratulations! All questions answered correctly!”;
}
alert(mistakes);
}
//–>
</script>
</head>
<body>
<p align=”center”>
<form method=”POST” name=”myquiz”>
<div class=”header”>Quiz<br><br></div>
<div class=”qheader”>
1) What is an ant?</div>
<div class=”qselections”>
<input type=”checkbox” value=”a” name=”question_0_0″>a) Animal<br>
<input type=”checkbox” value=”b” name=”question_0_1″>b) Piece of furniture<br>
<input type=”checkbox” value=”c” name=”question_0_2″>c) Insect<br>
<input type=”checkbox” value=”d” name=”question_0_3″>d) Drink<br>
</div>
<br>
<div class=”qheader”>
2) Which of these planets is closest to the sun?</div>
<div class=”qselections”>
<input type=”checkbox” value=”a” name=”question_1_0″>a) Pluto<br>
<input type=”checkbox” value=”b” name=”question_1_1″>b) Jupiter<br>
<input type=”checkbox” value=”c” name=”question_1_2″>c) Saturn<br>
<input type=”checkbox” value=”d” name=”question_1_3″>d) Venus<br>
</div>
<br>
<div class=”qheader”>
3) Which of these animals can fly</div>
<div class=”qselections”>
<input type=”checkbox” value=”a” name=”question_2_0″>a) Rhinoceros<br>
<input type=”checkbox” value=”b” name=”question_2_1″>b) Dog<br>
<input type=”checkbox” value=”c” name=”question_2_2″>c) Butterfly<br>
<input type=”checkbox” value=”d” name=”question_2_3″>d) Gorilla<br>
</div>
<input type=”button” value=”check” onclick=”checkAnswers()”>
</form>
<br>
</html>
[/CODE]

Thanks in advance

Rgds
Niall

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@PittimannMay 17.2004 — Hi!

Something like this:[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<style type="text/css">
<!--
.correct{
background-color:green;
width:250px;
}
.incorrect{
background-color:red;
width:250px;
}
-->
</style>
<script language="JavaScript" type="text/javascript">
<!--
var questions=new Array();
questions[0]=new Array(1,0,1,0);//first and third answer=correct, others=incorrect
questions[1]=new Array(0,0,0,1);//fourth answer=correct, others=incorrect
questions[2]=new Array(0,0,1,0);//third answer=correct, others=incorrect
function checkAnswers(){
var falseAnswers=new Array();
var answers=new Array();
for(var i=0;i<questions.length;i++){
answers[i]=new Array();
}
var boxArr=new Array();
var checkArrays=new Array();
var count=-1;
var cbox=document.getElementsByTagName('input');
for (var i=0;i<cbox.length;i++){
if(cbox[i].type=='checkbox'){
count++;
boxArr[count]=cbox[i].name;
}
}
for(var i=0;i<boxArr.length;i++){
checkArrays[i]=boxArr[i].split('_');
var quest=checkArrays[i][1];
for(var j=0;j<questions.length;j++){
if(quest==j){
if(document.myquiz['question_'+j+'_'+checkArrays[i][2]].checked){
var ins=1;
}
else{
var ins=0;
}
answers[j][checkArrays[i][2]]=(ins);
}
}
}
for(var j=0;j<questions.length;j++){
for(var k=0;k<questions[j].length;k++){
if (questions[j][k]==answers[j][k]){
document.getElementById('q_'+j+'_'+k).className='correct';
}
else{
document.getElementById('q_'+j+'_'+k).className='incorrect';
}
}
}
count=-1;
for(var j=0;j<questions.length;j++){
var answ=answers[j].toString();
var ques=questions[j].toString() ;
if(ques!=answ){
count++;
falseAnswers[count]='Mistake in question '+(j+1)+'n';
}
}
var mistakes="";
for(var j=0;j<falseAnswers.length;j++){
mistakes+=falseAnswers[j];
}
if (mistakes==""){
mistakes="Congratulations! All questions answered correctly!";
}
alert(mistakes);
}
//-->
</script>
</head>
<body>
<p align="center">
<form method="POST" name="myquiz">
<div class="header">Quiz<br><br></div>

<div class="qheader">
1) What is an ant?</div>
<div class="qselections">
<span id="q_0_0"><input type="checkbox" value="a" name="question_0_0">a) Animal</span><br>
<span id="q_0_1"><input type="checkbox" value="b" name="question_0_1">b) Piece of furniture</span><br>
<span id="q_0_2"><input type="checkbox" value="c" name="question_0_2">c) Insect</span><br>
<span id="q_0_3"><input type="checkbox" value="d" name="question_0_3">d) Drink</span><br>
</div>
<br>
<div class="qheader">
2) Which of these planets is closest to the sun?</div>
<div class="qselections">
<span id="q_1_0"><input type="checkbox" value="a" name="question_1_0">a) Pluto</span><br>
<span id="q_1_1"><input type="checkbox" value="b" name="question_1_1">b) Jupiter</span><br>
<span id="q_1_2"><input type="checkbox" value="c" name="question_1_2">c) Saturn</span><br>
<span id="q_1_3"><input type="checkbox" value="d" name="question_1_3">d) Venus</span><br>
</div>
<br>
<div class="qheader">
3) Which of these animals can fly</div>
<div class="qselections">
<span id="q_2_0"><input type="checkbox" value="a" name="question_2_0">a) Rhinoceros</span><br>
<span id="q_2_1"><input type="checkbox" value="b" name="question_2_1">b) Dog</span><br>
<span id="q_2_2"><input type="checkbox" value="c" name="question_2_2">c) Butterfly</span><br>
<span id="q_2_3"><input type="checkbox" value="d" name="question_2_3">d) Gorilla</span><br>
</div>
<input type="button" value="check" onclick="checkAnswers()">
</form>
<br>
</body>
</html>[/code]
Cheers - Pit
Copy linkTweet thisAlerts:
@niall_buckleyauthorMay 17.2004 — thanks very much pit...

I want to learn js and become fairly decent at implementing it in my programs...can you please advise me on what books to buy that will teach me from beginner level up to advanced level and that are fairly easy to understand....I would like to be a whizz at it if at all possible....

Rgds

Niall
Copy linkTweet thisAlerts:
@PittimannMay 17.2004 — Hi!

You're welcome. Unfortunately, I cannot advise you concerning books (I never used any as far as coding is concerned). For sure, other forum members can point you to something useful.

Cheers - Pit
×

Success!

Help @niall_buckley 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.19,
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,
)...