looking to create javascript quiz/questionaire

A

admin

Hi all,

First time poster here... I'm a webmaster and I'd like to add a simple
script to my website which will allow users to fill in a brief multiple
choice questionaire, and then provide a 'thoughful' suggestion based on
their answers.

In terms of the logic involved with the actual script: I've mulled it
over and thoroughly broken my brain, trying to devise a complicated
'scoring system' (with different answers giving different votes to
different outcomes) or an elimination system, with each answer
eliminating half of the possible outcomes, until there is only one
left. All of these inevitably elicit a "yeah, but what about this
case..."

I've finally decided that simpler is probably better... there are only
going to be a handful of questions, and I only have a handful of
outcomes, and I think I'd like to just provide my own answer, for each
unique combination of answers (there will be between 10-20 I think).

I'm struggling a bit with coding. I've looked at examples of
javascript quizzes, and mostly what I've found is stuff like this:

var score = 0;
ans[1] = "b";
ans[2] = "b";
ans[3] = "a";
ans[4] = "c";
ans[5] = "b";

accompanying this:

for(i=1;i<=5;i++){
answerText=answerText+"\nQuestion :"+i+"\n";
if(ans!=yourAns){
answerText=answerText+"\nThe correct answer was
"+ans+"\n"+explainAnswer+"\n";
}
else{
answerText=answerText+" \nCorrect! \n";
score++;

and:

answerText=answerText+"\n\nYour total score is : "+score+"\n";


I'm totally not up to speed with this stuff, but I'm eager to learn.
Deductively it looks like each question has a 'correct' answer in this
particular quiz and then it totals 'correct' answers. instead of this,
I want each possible string of answers to be a variable, at which point
the user is directed to an appropriate outcome page.
For example, AAAA = 1, AAAB = 2 AAAC = 3 and so on.

Does anyone have any scripts or any suggestions?

Thanks!

Chris
 
A

admin

Just an update, I've decided on the method I'd like to use.

Assuming there are 6 questions in my quiz, some with 2 possible
answers, some with 3 or 4... I assign each answer a 6 digit number.
then, I total them up.

Make sense? For question 1, in which the answer is either A) or B),
the variables would be 100,000 or 200,000... then for question 2, using
a 5 digit number, and so forth.

For example,

200,000 +
010,000 +
003,000 +
000,200 +
000,040 +
000,003 = 213,243, which is now a variable, that contains information
about how each of 6 questions was answered.

It appears I'll have 288 possible combinations, which I'll analyze
myself and offer a 'solution' for.

I'm still unsure about how to code it. How do I assign integer
variables for each of the possible answers, then give the instruction
to total them, then associate a 'solution' with each possible answer?
Any advice about how to get started would be greatly appreciated.

-Chris
 
R

Randy Webb

(e-mail address removed) said the following on 3/22/2006 8:05 PM:
Hi all,

First time poster here... I'm a webmaster and I'd like to add a simple
script to my website which will allow users to fill in a brief multiple
choice questionaire, and then provide a 'thoughful' suggestion based on
their answers.

It might be more involved than you think it is, depending on which way
you want it to work.
In terms of the logic involved with the actual script: I've mulled it
over and thoroughly broken my brain, trying to devise a complicated
'scoring system' (with different answers giving different votes to
different outcomes) or an elimination system, with each answer
eliminating half of the possible outcomes, until there is only one
left. All of these inevitably elicit a "yeah, but what about this
case..."

That's true with anything. Even this case :)
I've finally decided that simpler is probably better... there are only
going to be a handful of questions, and I only have a handful of
outcomes, and I think I'd like to just provide my own answer, for each
unique combination of answers (there will be between 10-20 I think).

How many possible outcomes depends on how many questions, and how many
answers those questions have. To get the possible outcomes, multiply the
number of answers for each question together. It gets quite large
quickly. Example:

Question 1: 3 Answers
Question 2: 2 Answers
Question 3: 5 Answers
Question 4: 3 Answers

How many possible outcomes? 3*2*5*3 = 90 and that's just 4 questions. It
could be argued that the actual number of outcomes to that is instead
288, based on the possibility that a question remains unanswered which
provides an additional "answer". Using Radio Buttons with one defaulted
to checked would alleviate that. Or, a Select list with no "Choose an
Answer" type options.

I'm totally not up to speed with this stuff, but I'm eager to learn.
Deductively it looks like each question has a 'correct' answer in this
particular quiz and then it totals 'correct' answers.

Yes, that is what the code did.
instead of this, I want each possible string of answers to
be a variable, at which point the user is directed to an
appropriate outcome page.
For example, AAAA = 1, AAAB = 2 AAAC = 3 and so on.

With 5 questions with A B C and D as possible answers, you have 1,024
possible outcomes. Not a very pleasing thought to have to create that
many pages of outcomes. Add one more question and your outcomes jump to
4,096. Give it an extra choice (E) then your 5 questions now have 3,125
possible outcomes. 6 questions with 5 possible answers is 15,625
possible outcomes.

10 questions with 6 answers each? 6^10 = 60,466,176 possible outcomes.

You ready to create over 60 million pages for a 10 question quiz?
Does anyone have any scripts or any suggestions?

Suggestion: You may want to re-think this project :)
 
R

Randy Webb

(e-mail address removed) said the following on 3/22/2006 9:58 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
Just an update, I've decided on the method I'd like to use.
Assuming there are 6 questions in my quiz, some with 2 possible
answers, some with 3 or 4... I assign each answer a 6 digit number.
then, I total them up.

OK, simple so far.
Make sense? For question 1, in which the answer is either A) or B),
the variables would be 100,000 or 200,000... then for question 2, using
a 5 digit number, and so forth.

It does have to be that difficult though. The value of an input element
is a string. When you add strings, you get a string:

"6" + "2" = "62"
6 + 2 = 8
For example,

200,000 +
010,000 +
003,000 +
000,200 +
000,040 +
000,003 = 213,243, which is now a variable, that contains information
about how each of 6 questions was answered.

"2" + "1" + "3" + "2" + "4" + 3" = "213243" which is what you need.
If you try to use numbers for it, you will have to convert it to a
number (after removing the commas), then add them together. Just use
string's and you don't have to worry with all of that.
It appears I'll have 288 possible combinations, which I'll analyze
myself and offer a 'solution' for.

How many possible combinations depends on how many answers each has but
if you came up with 288 I will trust that you knew how to figure it out.
I'm still unsure about how to code it. How do I assign integer
variables for each of the possible answers, then give the instruction
to total them, then associate a 'solution' with each possible answer?
Any advice about how to get started would be greatly appreciated.

Don't use integers, use the natural string value of inputs:

All of this could be done without client side scripting though. Submit
the form to the server, let the server total the answers and return the
appropriate page.

function getTotal(){
answer1 = document.myForm.question1.value;
answer2 = document.myForm.question2.value;
answer3 = document.myForm.question3.value;
answer4 = document.myForm.question4.value;
answer5 = document.myForm.question5.value;
answer6 = document.myForm.question6.value;
totalAnswer = answer1 + answer2 + answer3 + answer4 + answer5 + answer6;
alert(totalAnswer);
}

With this HTML:

<form name="myForm">
<select name="question1">
<option value="1">Answer 1</option>
<option value="2">Answer 2</option>
</select>
<select name="question2">
<option value="1">Answer 1</option>
<option value="2">Answer 2</option>
<option value="3">Answer 3</option>
</select>
<select name="question3">
<option value="1">Answer 1</option>
<option value="2">Answer 2</option>
<option value="3">Answer 3</option>
<option value="4">Answer 4</option>
</select>
<select name="question4">
<option value="1">Answer 1</option>
<option value="2">Answer 2</option>
</select>
<select name="question5">
<option value="1">Answer 1</option>
<option value="2">Answer 2</option>
<option value="3">Answer 3</option>
<option value="4">Answer 4</option>
<option value="5">Answer 5</option>
</select>
<select name="question6">
<option value="1">Answer 1</option>
<option value="2">Answer 2</option>
</select>

<input type="button" onclick="getTotal()" value="Get the Total">
</form>

As written, it will just give you the total. Change the
alert(totalAnswer) line to something like this:

document.location.href = totalAnswer + ".html";

Where each of your "possible combination" pages are named accordingly so
that page 124236.html is the page that has the combination for 1 2 4 2 3
6 combination.
 
A

admin

Randy,

Thanks for the reply... slimming the questionnaire as much as a I can
while still preserving something meaningful, it appears I'm now looking
at 144 combinations, with 9 possible solutions. It's up to me to sort
those 144 combinations by hand and assign a solution to it.

I'm pretty sure I'm going to have the solution appear in a frame,
that's pretty straightforward... I'll let you know how it goes!

C
 
A

admin

Still having trouble getting the thing working... I've got it to
recognize my .js file and respond to commands, but in the alert box,
instead of returning the getTotal, it says NaN.

I've changed it, so that instead of opening an alert box it re-directs
to a quizresults html page, and gave an instruction for the getTotal to
appear in a text area.... not working.

Have I screwed up the code? I've put up the bare bones on my webspace:
http://www.stacksback.com/pokerquiz/pokerquiz.html

C
 
R

Randy Webb

(e-mail address removed) said the following on 3/23/2006 2:43 AM:
Still having trouble getting the thing working... I've got it to
recognize my .js file and respond to commands, but in the alert box,
instead of returning the getTotal, it says NaN.

I've changed it, so that instead of opening an alert box it re-directs
to a quizresults html page, and gave an instruction for the getTotal to
appear in a text area.... not working.

Have I screwed up the code? I've put up the bare bones on my webspace:
http://www.stacksback.com/pokerquiz/pokerquiz.html

http://www.jibbering.com/faq/faq_notes/form_access.html#faBut

Radio buttons are the most common scenario where
document.formName.elementName.value won't give you the value of it. The
above URL explains it in depth. But, simply change your code to this:

function getRadioValue(radioReference)
{
var radioCollection, checkedButton;
var frm = document.forms["myForm"];
if(frm)
{
radioCollection = frm.elements[radioReference];
if(radioCollection)
{
for(var c = 0;c < radioCollection.length;c++)
{
if(radioCollection[c].checked)
{
checkedButton = radioCollection[c];
break;
}
}
if(checkedButton)
{
return checkedButton.value;
}
}
}
}

function getTotal(){
var answer1 = getRadioValue("question1");
var answer2 = getRadioValue("question2");
var answer3 = getRadioValue("question3");
var answer4 = getRadioValue("question4");
var answer5 = getRadioValue("question5");
var answer6 = getRadioValue("question6");
var totalAnswer = answer1 + answer2 + answer3 + answer4 + answer5 +
answer6;
window.location="pokerquizresults.html?" + totalAnswer;
}

When pokerquizresults.html is loaded the URL will look like this:

pokerquizresults.html?123456

Where the 123456 is the answer you are looking for. Then,
pokerquizresults.html can get the value via script:

<script type="text/javascript">
totalAnswerInURL = document.location.search.substring(1);
</script>
 
A

admin

Randy said:
Radio buttons are the most common scenario where
document.formName.elementName.value won't give you the value of it. The
above URL explains it in depth. But, simply change your code to this:

function getRadioValue(radioReference)
{
var radioCollection, checkedButton;
var frm = document.forms["myForm"];
if(frm)
{
radioCollection = frm.elements[radioReference];
if(radioCollection)
{
for(var c = 0;c < radioCollection.length;c++)
{
if(radioCollection[c].checked)
{
checkedButton = radioCollection[c];
break;
}
}
if(checkedButton)
{
return checkedButton.value;
}
}
}
}

function getTotal(){
var answer1 = getRadioValue("question1");
var answer2 = getRadioValue("question2");
var answer3 = getRadioValue("question3");
var answer4 = getRadioValue("question4");
var answer5 = getRadioValue("question5");
var answer6 = getRadioValue("question6");
var totalAnswer = answer1 + answer2 + answer3 + answer4 + answer5 +
answer6;
window.location="pokerquizresults.html?" + totalAnswer;
}

Done and done.

When pokerquizresults.html is loaded the URL will look like this:

pokerquizresults.html?123456

Where the 123456 is the answer you are looking for. Then,
pokerquizresults.html can get the value via script:

<script type="text/javascript">
totalAnswerInURL = document.location.search.substring(1);
</script>

Progress! :) The javascript works and it returns a string of numbers
which tells me how the quiz was filled in. the URL returned is
pokerquizresults.html?123456

I'm still unsure about how to get the solution to appear in the text
box on my page... and it doesn't appear to matter if i put that piece
of code you gave me in the body or the header of pokerquizresults.html
(?) This piece of code I mean:

<script type="text/javascript">
totalAnswerInURL = document.location.search.substring(1);
</script>

One other thing, I don't want each unique string to be a new html page;
but rather, to point to one of about 6 'solution' pages that i'm going
to create. solution1.html, solution2.html, etc. I'll list all of the
6-digit strings that are associated with each solution page. Could you
tell me how I'd add this instruction?

C


 
R

Randy Webb

(e-mail address removed) said the following on 3/23/2006 5:42 PM:
Randy Webb wrote:


Progress! :) The javascript works and it returns a string of numbers
which tells me how the quiz was filled in. the URL returned is
pokerquizresults.html?123456

I'm still unsure about how to get the solution to appear in the text
box on my page... and it doesn't appear to matter if i put that piece
of code you gave me in the body or the header of pokerquizresults.html
(?) This piece of code I mean:

<script type="text/javascript">
totalAnswerInURL = document.location.search.substring(1);
</script>

<script type="text/javscript">
window.onload = setTextField;
function setTextField(){
totalAnswerInURL = = document.location.search.substring(1);
document.myForm.myInput.value = totalAnswerInURL;
}
</script>

<form name="myForm">
<input type="text" name="myInput">
One other thing, I don't want each unique string to be a new html page;
but rather, to point to one of about 6 'solution' pages that i'm going
to create. solution1.html, solution2.html, etc. I'll list all of the
6-digit strings that are associated with each solution page. Could you
tell me how I'd add this instruction?

That is better done in the quiz page instead of the second page.

var myObject = new Object();
myObject[111111] = "page1.html";
myObject[111112] = "page2.html";
.....
And so on so that you have an entry for every possible combination.
In this format:

myObject[combination] = "page to go to for this combo";

And then change your function in the quiz page:

function getTotal(){
.......
window.location = myObject[totalAnswer];
}
 
A

admin

Randy,

Thanks! I'll put it in place today. The project has grown and some of
it is now being accomplished by php.. but the java code is really going
to help with interactivity on my site and I can now quickly and easily
provide specific content based on user-specified criteria... help much
appreciated,

Chris
 
R

Randy Webb

(e-mail address removed) said the following on 3/24/2006 4:51 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

Randy,

Thanks! I'll put it in place today. The project has grown and some of
it is now being accomplished by php..

If you have PHP, then the entire thing can be done with PHP and it won't
be JS Dependent. Submit the form and let PHP do the calculation and then
return the proper page.

Java != Javascript
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top