Welcome to Dream.In.Code
Getting Help is Easy!

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!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Randomising Form Field Order

 
Reply to this topicStart new topic

Randomising Form Field Order

Gufftree
post 6 May, 2008 - 01:43 AM
Post #1


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 6



Hi all,

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?

CODE
<FORM>
<b>Question One</b><br>
<input type=radio name="question1" value="a" onClick="Calculate(4, this.value)">Answer A<br>
<input type=radio name="question1" value="b" onClick="Calculate(4, this.value)">Answer B<br>
<input type=radio name="question1" value="c" onClick="Calculate(4, this.value)">Answer C<br>
<input type=radio name="question1" value="d" onClick="Calculate(4, this.value)">Answer D<p>

<b>Question Two</b><br>
<input type=radio name="question2" value="a" onClick="Calculate(5, this.value)">Answer A<br>
<input type=radio name="question2" value="b" onClick="Calculate(5, this.value)">Answer B<br>
<input type=radio name="question2" value="c" onClick="Calculate(5, this.value)">Answer C<br>
<input type=radio name="question2" value="d" onClick="Calculate(5, this.value)">Answer D<p>

<input type=button onClick="Score()" value="Submit Quiz">
</FORM>


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.

Any tips or hints would be much appreciated!

Thanks smile.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


no2pencil
post 6 May, 2008 - 01:53 AM
Post #2


DIC K-mart

Group Icon
Joined: 10 May, 2007
Posts: 3,324

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.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Gufftree
post 6 May, 2008 - 01:57 AM
Post #3


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 6

QUOTE(no2pencil @ 6 May, 2008 - 01:53 AM) *

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 tongue.gif

This post has been edited by Gufftree: 6 May, 2008 - 01:58 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Gufftree
post 6 May, 2008 - 06:48 PM
Post #4


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 6

no2pencil, Is there a way to keep my answers in an array and still use that random function?

Sorry, i'm still a beginner when it comes to Javascript and programming tongue.gif So use small words, haha.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

no2pencil
post 6 May, 2008 - 09:08 PM
Post #5


DIC K-mart

Group Icon
Joined: 10 May, 2007
Posts: 3,324

QUOTE(Gufftree @ 6 May, 2008 - 09:48 PM) *

no2pencil, Is there a way to keep my answers in an array and still use that random function?


Random number within an array icon_up.gif

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;

document.write(movie[rand_no]);
-->
</script>
</body></html>
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Gufftree
post 6 May, 2008 - 09:45 PM
Post #6


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 6

QUOTE(no2pencil @ 6 May, 2008 - 09:08 PM) *

QUOTE(Gufftree @ 6 May, 2008 - 09:48 PM) *

no2pencil, Is there a way to keep my answers in an array and still use that random function?


Random number within an array icon_up.gif

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;

document.write(movie[rand_no]);
-->
</script>
</body></html>



Thanks.. doesn't look too difficult.

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.

This is confusing blink.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Gufftree
post 7 May, 2008 - 02:22 AM
Post #7


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 6

Actually why don't I just show you my entire script so you know what I mean tongue.gif

CODE
<HTML>

<HEAD>

<script LANGUAGE="JavaScript">

var answer = new Array;
var yourAnswer = new Array;
var score = 0;

answer[1] = "a";
answer[2] = "c";
answer[3] = "a";
answer[4] = "d";
answer[5] = "b";

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);

}
//  End -->
</script>

</HEAD>
<BODY>
<DIV ALIGN="CENTER">
<h1>Quiz!</h1>
<b>Test your knowledge!</b>
<hr>
<FORM>
<b>Question 1. </b><br>
<input type=radio name="question1" value="a" onClick="Calculate(1, this.value)">Qa)<br>
<input type=radio name="question1" value="b" onClick="Calculate(1, this.value)">Qb)<br>
<input type=radio name="question1" value="c" onClick="Calculate(1, this.value)">Qc)<br>
<input type=radio name="question1" value="d" onClick="Calculate(1, this.value)">Qd)<p>

<b>Question 2. </b><br>
<input type=radio name="question2" value="a" onClick="Calculate(2, this.value)">Qa)<br>
<input type=radio name="question2" value="b" onClick="Calculate(2, this.value)">Qb)<br>
<input type=radio name="question2" value="c" onClick="Calculate(2, this.value)">Qc)<br>
<input type=radio name="question2" value="d" onClick="Calculate(2, this.value)">Qd)<p>

<b>Question 3.</b><br>
<input type=radio name="question3" value="a" onClick="Calculate(3, this.value)">Qa)<br>
<input type=radio name="question3" value="b" onClick="Calculate(3, this.value)">Qb)<br>
<input type=radio name="question3" value="c" onClick="Calculate(3, this.value)">Qc)<br>
<input type=radio name="question3" value="d" onClick="Calculate(3, this.value)">Qd)<p>

<b>Question 4. </b><br>
<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qa)<br>
<input type=radio name="question4" value="b" onClick="Calculate(4, this.value)">Qb)<br>
<input type=radio name="question4" value="c" onClick="Calculate(4, this.value)">Qc)<br>
<input type=radio name="question4" value="d" onClick="Calculate(4, this.value)">Qd)<p>

<b>Question 5. </b><br>
<input type=radio name="question5" value="a" onClick="Calculate(5, this.value)">Qa)<br>
<input type=radio name="question5" value="b" onClick="Calculate(5, this.value)">Qb)<br>
<input type=radio name="question5" value="c" onClick="Calculate(5, this.value)">Qc) <br>
<input type=radio name="question5" value="d" onClick="Calculate(5, this.value)">Qd) <p>

<CENTER>
<input type=button onClick="Score()" value="Submit Quiz">
</CENTER>
</FORM>
</DIV>

</BODY>
</HTML>


So basically I need to randomise the question order without modifying that script too much..

Possible?

This post has been edited by Gufftree: 7 May, 2008 - 02:23 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

no2pencil
post 7 May, 2008 - 02:56 AM
Post #8


DIC K-mart

Group Icon
Joined: 10 May, 2007
Posts: 3,324

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);

}
//  End -->
</script>

</HEAD>
<BODY>
<DIV ALIGN="CENTER">
<h1>Quiz!</h1>
<b>Test your knowledge!</b>
<hr>
<FORM>
<b>Question 1. </b>
<script language=JavaScript>
get_rand();
</script>
<br>
<script language=JavaScript>
place_answer();
</script>


<b>Question 2. </b>
<script language=JavaScript>
get_rand();
</script>
<br>
<input type=radio name="question2" value="a" onClick="Calculate(2, this.value)">Qa)<br>
<input type=radio name="question2" value="b" onClick="Calculate(2, this.value)">Qb)<br>
<input type=radio name="question2" value="c" onClick="Calculate(2, this.value)">Qc)<br>
<input type=radio name="question2" value="d" onClick="Calculate(2, this.value)">Qd)<p>

<b>Question 3.</b>
<script language=JavaScript>
get_rand();
</script>
<br>
<input type=radio name="question3" value="a" onClick="Calculate(3, this.value)">Qa)<br>
<input type=radio name="question3" value="b" onClick="Calculate(3, this.value)">Qb)<br>
<input type=radio name="question3" value="c" onClick="Calculate(3, this.value)">Qc)<br>
<input type=radio name="question3" value="d" onClick="Calculate(3, this.value)">Qd)<p>

<b>Question 4. </b>
<script language=JavaScript>
<!--
get_rand();
var pos = place_answer();
document.write('<br>');
for(i=1;i<5;i++){
    if(pos==i) {
        if(i==1) document.write('<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qa)'+answer[i]+'<br>');
        if(i==2) document.write('<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qb)'+answer[i]+'<br>');
        if(i==3) document.write('<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qc)'+answer[i]+'<br>');
        if(i==4) document.write('<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qd)'+answer[i]+'<br>');
    }
    else {
        if(i==1) document.write('<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qa)<br>');
        if(i==2) document.write('<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qb)<br>');
        if(i==3) document.write('<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qc)<br>');
        if(i==4) document.write('<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qd)<br>');
    }
}
-->
</script>
<br>
<!--
<input type=radio name="question4" value="a" onClick="Calculate(4, this.value)">Qa)<br>
<input type=radio name="question4" value="b" onClick="Calculate(4, this.value)">Qb)<br>
<input type=radio name="question4" value="c" onClick="Calculate(4, this.value)">Qc)<br>
<input type=radio name="question4" value="d" onClick="Calculate(4, this.value)">Qd)<p>
-->
<b>Question 5. </b>
<script language=JavaScript>
get_rand();
</script>
<br>
<input type=radio name="question5" value="a" onClick="Calculate(5, this.value)">Qa)<br>
<input type=radio name="question5" value="b" onClick="Calculate(5, this.value)">Qb)<br>
<input type=radio name="question5" value="c" onClick="Calculate(5, this.value)">Qc) <br>
<input type=radio name="question5" value="d" onClick="Calculate(5, this.value)">Qd) <p>

<CENTER>
<input type=button onClick="Score()" value="Submit Quiz">
</CENTER>
</FORM>
</DIV>

</BODY>
</HTML>


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.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Gufftree
post 7 May, 2008 - 03:06 AM
Post #9


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 6

That is perfect, thanks so much! biggrin.gif I appreciate the help.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 06:23AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month