JavaScript "Cosmo" quiz request

I’m looking at setting up a “Cosmo”-style quiz on a webpage. The sort where the answer is “mostly As - you’re good in bed” “mostly Bs - try harder” etc.

I can do JavaScript but my little brain isn’t a very good progammer, and I cannot for the life of me work out how to tot it up. The intention is that it just pops up an alert box with the analysis when they hit “submit”.

There are five questions, each of which has four radio buttons, which always represent the answer A, B, C or D. I was trying to be clever and assign a numerical value to each one, in the hope that there would be thresholds that would definitely indicate a higher number of As or Bs etc., but that doesn’t seem to be working.

Anyone got a sample of a quiz that does this already? Or can someone help me construct my own?

Thanks!

What about it “isn’t working?” The totalling of the numbers, the popping up the alert box, or something else? These are all completely different problems that have to be solved in order for the whole thing to work. (Fortunately, they’re all very easy.)

Can you post what you have already?

Here is a guess, but I would like to see your code. If you include prototype.js (available at www.prototypejs.org), $$(‘className’) means get all elements whose css selector is .className and $F() is the same as getting the value property of an input.

<script language=“javascript” type=“text/javascript” >
// <![CDATA[

var getTotalScore = function()
{
var totalNumberOfQuestions = whatever;
var total = 0;
for (var i = 1; i <= totalNumberOfQuestions; i++)
{
total += $F($$(‘input.question’ + i).find(function(answerInput)
{
return answerInput.checked == ‘checked’;
}));
}
return total;
};

// ]]>

<input type=“radio” id=“question1” value=“1”/>A
<input type=“radio” id=“question1” value=“2”/>B
<input type=“radio” id=“question1” value=“3”/>C
<input type=“radio” id=“question1” value=“4”/>D

<input type=“radio” id=“question2” value=“1”/>A
<input type=“radio” id=“question2” value=“2”/>B
<input type=“radio” id=“question2” value=“3”/>C
<input type=“radio” id=“question2” value=“4”/>D
Rob

Hi, thanks for your replies. I didn’t explain myself very well. What I meant was that I understand the syntax of JavaScript very well, but just don’t have the mental capacity to work out the logic for this exercise. In other words, what are the best principles for this (apparently very simple!) task, that I can then apply using JS?

Find which radio button is checked for each question and accumulate the values of the checked buttons.

Rob

Keep in mind that just adding up the values only works if your answers ABCD are a spectrum that basically represents one trait, e.g. A is “lazy” and D is “hard-working” and B and C are just different degrees of those. If the four choices are actually kind of independent, and you truly do want to know which choice the person made the most, then you need to loop through the items and keep four tallies, incrementing the appropriate tally by one for each corresponding choice.

To see the difference, imagine that you answered half A and half C. Do you want the results to read like you answered mostly A and C, or that you answered B on average?