PDA

View Full Version : Quiz code



Liam
12-17-2009, 05:05 PM
Hi every one :)

I am currently working on a quiz for a project. The code works but I want to add something else to it that I can't work out how to do. I want the quiz to mark the code and if the user gets over 80%a button is displayed allowing the users to move onto the next section. Any ideas? here is the code:


// Function to create arrays.
function makeArray(len) {
for (var i = 0; i < len; i++) this[i] = null;
this.length = len;
}

// creates objects for this question.
function makeQuestion(question, correctAnswer) {
var args = makeQuestion.arguments;
this.question = question;
this.correctAnswer = correctAnswer;
this.userAnswer = null;
this.isCorrect = isCorrect;
this.showForm = showForm;
this.userChoices = new makeArray(args.length - 2);
for (var i = 0; i < args.length - 2; i++) {
this.userChoices[i] = args[i + 2];
}
}

// determins if the questions answered correctly
function isCorrect() {
if (this.correctAnswer == this.userAnswer) return true;
else return false;
}

// Method to display contents of question object.
function showForm(n) { //argument n is the number of the question.
document.write((n + 1) + '. ' + this.question + '<BLOCKQUOTE><FORM>'); //n+1 = the number of the question in the array (has to add one because the array starts with 0 or the first question would be number 0, this question displays the questions text, blockquote off sets the answer making them indented.
for (var i = 0; i < this.userChoices.length; i++) { //goes through the questions one by one.
document.write('<INPUT TYPE = "RADIO" NAME = "q' + n + //create the radio buttons.
'" onClick = "quiz[' + n + '].userAnswer = ' + i + '">');
document.write(this.userChoices[i] + '<BR>');
}
document.write('</FORM></BLOCKQUOTE>');
}

// This corrects the quiz, dispays the correct answers and the score.
function correctQuiz() {
var correct = 0; // sets the correct page to a score of zero
correctPage = '<HTML><TITLE>Corrections</TITLE><BODY BGCOLOR = "#FFFFFF">'; // sets the attributes of the correction page
for (var i = 0; i < quiz.length; i++) { //loop runs through questions
if (quiz[i].isCorrect()) correct++; // the is correct starts at 0 every time is correct returns true (an answer is correct) it adds 1 to the number adding up correct answers.
}
var score = Math.round((correct / quiz.length) * 100); // find out the % of correct answers by using math and the % equation
correctPage += 'Score: <STRONG>' + score + '</STRONG> %';
if (correct < quiz.length) { //if the correct number is less than the quiz length then not all the answers were correct if it is the same then all ansers were correct.
correctPage += ifWrong; // if there were wrong answers
for (var i = 0; i < quiz.length; i++) { // starts a for loop through the answers
if (!quiz[i].isCorrect()) { // if quiz doesn't = to iscorrect.
correctPage += (i + 1) + '. ' + // this shows what number was wrong.
quiz[i].userChoices[quiz[i].correctAnswer] + '<BR>'; // displays the correct answes for the questions that were wrong
}
}
}
else correctPage += ifAced; // this is the correct page displayed if all the answers were right.
correctPage += '</BODY></HTML>'; // opens the window of correct page
correctwin = window.open ('', '', 'height=300,width=300,scrollbars=yes');
if (correctwin.opener == null) correctwin.opener = window;
correctwin.location = 'javascript:opener.correctPage';
}

// Message to display if quiz is aced.
var ifAced = "<P>Wow you really were paying attention well done you ACED it.<P> please click here to carry on";
// Message to display if any are wrong.
var ifWrong = "<P>FAILED!!!!!! did you fall asleep whilst you were browsing my site??? Don't bother retaking it here are the answers!<P>";

// Create seven question objects...
quiz = new makeArray(8); // this is how many questions there is in the quiz
quiz[0] = new makeQuestion("What is the correct name for the leg marking found in picture 1?", // 1st question starts at 0 because thats the first number in an array
2, // correct answer
"Sock", // choice 0
"Stocking", // choice 1
"Coronet band", // choice 2
"half cannon"); // choice 3

quiz[1] = new makeQuestion("What is the correct name for the leg marking found in picture 2?", // 2nd question
0, // correct answer
"Sock", // choice 0
"Stocking", // choice 1
"Coronet band", //choice 2
"half cannon"); //choice 3

quiz[2] = new makeQuestion("What is the correct name for the leg marking found in picture 3?",
1, // correct answer
"Sock", // choice 0
"Stocking", // choice 1
"Coronet band", //choice 2
"half cannon");

quiz[3] = new makeQuestion("What is the correct name for the leg marking found in picture 4?",
3, // correct answer
"Sock", // choice 0
"Stocking", // choice 1
"Coronet band", //choice 2
"half cannon");

quiz[4] = new makeQuestion("What is the correct name for the face marking found in picture 5?",
2,
"Bald",
"Star",
"Snip",
"Blaze");

quiz[5] = new makeQuestion("what is the correct name for the face marking found in picture 6?",
0,
"Bald",
"Star",
"Snip",
"Blaze");

quiz[6] = new makeQuestion("what is the correct name for the face marking found in picture 7?",
3,
"Bald",
"Star",
"Snip",
"Blaze");
quiz[7] = new makeQuestion("what is the correct name for the face marking found in picture 8?",
1,
"Bald",
"Star",
"Snip",
"Blaze");