WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Making a quiz with javascript on Fri Jul 23, 2010 11:54 pm

somethingnew12


Registered Member
Registered Member
hello...

A friend told me i can get help here if i need so here it comes..

I want to make a quiz with 7 question and under each quistion there are 3 radiobuttons to put you answer. To the end, therw would be a button, and when someone press that a alert will popup saying how many you got right..

Thanks for the help.

2 Re: Making a quiz with javascript on Sat Jul 24, 2010 1:22 pm

ankillien

avatar
Administrator
Administrator
Hey there Smile

This code should do the trick....

Code:
<html>
<head>
<title>Play Quiz</title>
<script language="JavaScript">
//redirects if 75% or greater, returns false otherwise
function checkAnswers(){

//put all the question sets into this array
var allQuestions = new Array(document.myForm.q1,
                            document.myForm.q2,
                            document.myForm.q3,
                            document.myForm.q4,
                            document.myForm.q5,
                            document.myForm.q6,
                            document.myForm.q7);
                           
 var totalScore = 0; //initialize to 0

 //go through each question set
 for (var i in allQuestions) {
  var temp = allQuestions[i];

  //go through each radio button in the current question set
  for (var j = 0; j < temp.length; j++) {

    //if the correct one is chosen then add 1 to total score
    if (temp[j].value == "correct" && temp[j].checked == true) {
      totalScore++;
    }
  }
 }
 alert (totalScore + ' Correct Answer(s)');
}
</script>
</head>
<body>
<form name="myForm">
 <!-- the only thing to note here is that the correct answer
 has a value set to "correct" while incorrect ones do not-->
 Question 1: Type question 1?<br>
 <input type="radio" name="q1" value="correct">A<br>
 <input type="radio" name="q1">B<br>
 <input type="radio" name="q1">C<br>
 <br>
 <br>
 Question 2: Type question 2?<br>
 <input type="radio" name="q2" value="correct">A<br>
 <input type="radio" name="q2">B<br>
 <input type="radio" name="q2">C<br>
 <br>
 <br>
 Question 3: Type question 3?<br>
 <input type="radio" name="q3" value="correct">A<br>
 <input type="radio" name="q3">B<br>
 <input type="radio" name="q3">C<br>
 <br>
Question 4: Type question 4?<br>
 <input type="radio" name="q4"value="correct">A<br>
 <input type="radio" name="q4">B<br>
 <input type="radio" name="q4">C<br>
 <br>
 <br>
 Question 5: Type question 5?<br>
 <input type="radio" name="q5" value="correct">A<br>
 <input type="radio" name="q5">B<br>
 <input type="radio" name="q5">C<br>
 <br>
 <br>
 Question 6: Type question 6?<br>
 <input type="radio" name="q6" value="correct">A<br>
 <input type="radio" name="q6">B<br>
 <input type="radio" name="q6">C<br>
 <br>
  Question 7: Type question 7?<br>
 <input type="radio" name="q7" value="correct">A<br>
 <input type="radio" name="q7">B<br>
 <input type="radio" name="q7">C<br>
 <br>

<button onclick="return checkAnswers()">Get the result</button> <input type="reset" value="Reset answers" />
 
</form>
</body>

</html>

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum