473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

looking to create javascript quiz/questionaire

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[i]!=yourAns[i]){
answerText=answerText+"\nThe correct answer was
"+ans[i]+"\n"+explainAnswer[i]+"\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

Mar 23 '06 #1
11 3274
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

Mar 23 '06 #2
ad***@stacksback.com 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.

<snip>
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 :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 23 '06 #3
ad***@stacksback.com 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.
<URL: http://www.safalra.com/special/googlegroupsreply/ >
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 23 '06 #4
Randy Webb said the following on 3/22/2006 10:50 PM:

<snip>
It does have to be that difficult though.


Should be:
It doesn't have to be that difficult though.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 23 '06 #5
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

Mar 23 '06 #6
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

Mar 23 '06 #7
ad***@stacksback.com 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_not...ess.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>

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 23 '06 #8

Randy Webb wrote:
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
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Mar 23 '06 #9
ad***@stacksback.com said the following on 3/23/2006 5:42 PM:
Randy Webb wrote:
<snip>
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>


<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">
</form>
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];
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 24 '06 #10
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

Mar 24 '06 #11
ad***@stacksback.com 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.

<URL: http://www.safalra.com/special/googlegroupsreply/ >
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

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 25 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: C. David Rossen | last post by:
Hello: I found a script for a simple multiple choice quiz that I would like to tailor to a quiz that I want to post on a website. The quiz I want to use can be found at : ...
17
by: rgoya | last post by:
Peace be with you! About a year ago, I created a JavaScript which turns any web site into an online anti-war protest: http://www.geocities.com/rgoya/javascript/PROTEST.HTM I have recently...
6
by: Vandana Rola | last post by:
Hello Everyone, I posted this question earlier under creating Multiple choice quiz. Is it possible to ignore something using javascript. What I am trying to do is creating a multiple answer...
1
by: number1.email | last post by:
Hello, I have a simple Web Page Questionairre in which questions are read from a database, and the user can indicate the correct answer via either a radio input control or a dropdown list. The...
2
by: kenny | last post by:
I'm making a quiz to be posted on the internet. but I'm facing difficulties in finding a simple timer for the quiz (unlimited timing) which will keep on running regardless to the change of the page...
2
by: Surya Vellanki | last post by:
Hi all, I have been struggling with this problem from a few days. I hope I will get some help here. I am developing an online quiz portal with ASP.NET 2.0 and I am using Atlas framework for...
5
by: pefok | last post by:
Hello everyone, One of my course works is to implement a java based quiz system. the program consists of two modules one for the teacher to create, set questions , answers and the number of...
8
by: danjoplin | last post by:
I've only recently started javascript and I'm a java programmer so I'm sure I've done something obvious that I can't see. Basically this goes with an html document which has a number of questions on...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.