Join 86,399 Programmers. There are 1,460 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
i'm a new member. I was researching an answer to a Javascript related question and came across this forum.
Basically I have a quiz and I would like the order of the questions to randomise whenever the page is refreshed. I'll post the form part of the script as an example of what I have already. Would anyone be able to point me in the right direction? Is there a way to do it?
Obviously I have the Javascript which calculates the correct answers etc but I don't think I need to include it in this post for the purposes of my question.
I would store all of the questions & answers in a flat text file, & use the random function in this snippet to randomly load the questing with equivalent answer.
I would store all of the questions & answers in a flat text file, & use the random function in this snippet to randomly load the questing with equivalent answer.
Hrm well I have all of the script set up to store all of the questions and the answers so I don't really want to change it. I'm just using arrays, it's pretty simple but it's all I need. Is there no way to just randomise the order of each radio button + its corrosponding text? I'm just hoping for an easy way out here so I don't need to make any major changes
This post has been edited by Gufftree: 6 May, 2008 - 01:58 AM
no2pencil, Is there a way to keep my answers in an array and still use that random function?
Random number within an array
CODE
<html> <head> <title>JavaScript Random Numbers</title> </head> <body> <script language="JavaScript"> <!-- movie = new Array movie[1]="Casablanca" movie[2]="The Wizard of Oz" movie[3]="The Dirty Dozen" movie[4]="Who Framed Roger Rabbit?" movie[5]="The Five Heartbeats" movie[6]="Battleground" movie[7]="The Life and Times of Hank Greenberg" movie[8]="The Battle for Heavy Water" movie[9]="My Blue Heaven"
var rand_no = Math.floor((9-1)*Math.random()) + 1;
no2pencil, Is there a way to keep my answers in an array and still use that random function?
Random number within an array
CODE
<html> <head> <title>JavaScript Random Numbers</title> </head> <body> <script language="JavaScript"> <!-- movie = new Array movie[1]="Casablanca" movie[2]="The Wizard of Oz" movie[3]="The Dirty Dozen" movie[4]="Who Framed Roger Rabbit?" movie[5]="The Five Heartbeats" movie[6]="Battleground" movie[7]="The Life and Times of Hank Greenberg" movie[8]="The Battle for Heavy Water" movie[9]="My Blue Heaven"
var rand_no = Math.floor((9-1)*Math.random()) + 1;
But that won't change the question order on the page around will it? I have my answers stored in arrays, but the actual questions and radio buttons are just HTML as I posted above.
function Calculate(question, answer) { yourAnswer[question]=answer; }
function Score(){ var answerStore = "What did you score?\n------------------------------------\n"; for(i=1;i<=5;i++){ answerStore=answerStore+"\nQuestion: "+i+"\n"; if(answer[i]!=yourAnswer[i]){ answerStore=answerStore+"The correct answer was "+answer[i]+"\n"; } else{ answerStore=answerStore+"Correct! \n"; score++; } }
answerStore=answerStore+"\n\nYou scored: "+score+"\n"; answerStore=answerStore+""; if(score<=0){ answerStore=answerStore+"You didn't do too well..."; } if(score>=1 && score <=2){ answerStore=answerStore+"You need a bit of practise."; } if(score>=3 && score <=4){ answerStore=answerStore+"You're doing pretty good!"; } if(score>4){ answerStore=answerStore+"Top marks! Well done!"; } alert(answerStore);
This isn't 100% working, I'm sure you can figure out the details...
CODE
<HTML>
<HEAD> <script LANGUAGE="JavaScript">
function get_rand() { <!-- var rand_question = Math.floor((9-1)*Math.random()) + 1; document.write(question[rand_question]); --> }
function place_answer() { <!-- var rand_position = Math.floor((4-1)*Math.random()) + 1; return rand_position; //answer[rand_position]; --> }
<!-- Declare questions array --> question = new Array question[1]="What is the opposite of up?" question[2]="What is the opposite of down?" question[3]="Who wrote the Hobbit?" question[4]="Why is the sky blue?" question[5]="What year was America Discovered?" question[6]="What baseball team did Mickey Mantle play for?" question[7]="How many seasons did the show Seinfeld last?" question[8]="What is the generic version of Transformers?" question[9]="How many questions are on this test?"
answer = new Array answer[1]="Down" answer[2]="Up" answer[3]="J.R.R. Tolken" answer[4]="Reflection of the Ocean" answer[5]="1492" answer[6]="New York Yakees" answer[7]="9" answer[8]="Go Bots" answer[9]="9"
<!-- Grab a random question --> var rand_question = Math.floor((9-1)*Math.random()) + 1; <!-- End question section -->
<!-- Declare answer array. score is where your score is stored. -->
var _answer = new Array; var yourAnswer = new Array; var score = 0;
<!-- This is where the letter corrosponding to the correct answer is stored --> _answer[1] = "a"; _answer[2] = "c"; _answer[3] = "a"; _answer[4] = "d"; _answer[5] = "b";
<!-- This is a function to store your answer. It is used in the body with the form itself using "onClick="Calculate(2, this.value)" 2 being the question number, and this value being whatever radio button was clicked on the form--> function Calculate(question, answer) { yourAnswer[question]=answer; }
function Score(){ <!-- The variable answerStore stores all information related to the answer. It starts as "What did you score?" then the question number is added. And if the question is right, the text "Correct!" is added. This is all outputted in an alert box when "Submit quiz" is clicked--> var answerStore = "What did you score?\n------------------------------------\n"; <!--The loop will continue and increment "i" by 1 until it equals 4--> for(i=1;i<=5;i++){ answerStore=answerStore+"\nQuestion: "+i+"\n"; <!-- This is calculated if answer is equal to your answer. It determines the question by "i" which is calculated above. If it is not equal to your answer, it carriers out the line below.) if(answer[i]!=yourAnswer[i]){ <!-- If the answer was wrong, then this line is carried out. The text "The Correct answer was" plus the answer number are stored in answerStore--> answerStore=answerStore+"The correct answer was "+answer[i]+"\n"; } else{ <!--Otherwise your answer is right, so it states that it is correct, and increments your score by one and stores "Correct!" in answerStore.--> answerStore=answerStore+"Correct! \n"; <!-- score is incremented by 1 --> score++; } }
<!-- Now that score has been calculated, it is added to the answerStore variable. It's getting bigger! --> answerStore=answerStore+"\n\nYou scored: "+score+"\n";
<!--Now we tell the user what they scored! This is a simple series of if statements.--> answerStore=answerStore+""; <!--If score is less than or equal to 0, output the statement--> if(score<=0){ answerStore=answerStore+"You didn't do too well..."; } <!-- If score is greater than or equal to 1, and less than or equal to 2, output the statement--> if(score>=1 && score <=2){ answerStore=answerStore+"You need a bit of practise."; } <!-- If score is greater than or equal to 3 and less than or equal to 4, output the statement--> if(score>=3 && score <=4){ answerStore=answerStore+"You're doing pretty good!"; } <!--If the score is greater than 4, output the statement--> if(score>4){ answerStore=answerStore+"Top marks! Well done!"; } <!--Open an alert box with the contents of answerStore--> alert(answerStore);
I have to get going really quick, & this is all the more I could get done. The answer does not match the question... but again, I'm sure you can figure this out.