JavaScript for Quiz

They have: 46 posts

Joined: Aug 2001

Hi all,

My question is somewhat similar to Taff's but I know less than the basics about JavaScript - only ever copied and pasted scripts from the freebie pages before and played with them a little if need be.

My problem is that I need to write the script to add up the scores for a quiz. In this case answer (a) is worth 1, (b) is worth 3 and (c) is worth 2.

Can anyone help me with what needs to go into this? Please?

This is what I have so far ...

and so on up to 10 questions -
within the form tags and all the associated stuff.

I have gone to javascriptworld.com (no quiz script) and the freebie sites that have one or two quizzes on them (they are not similar) and otherwise searched the web. The answer is to be displayed on the next page which people click through to.

Can anyone help with what I have to do?

Thanking you in advance.

Annette
Confused

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

You can use PHP or Perl, if you're going to go to another page.

With JavaScript, you can have it report immediately.

Anyway, take the sum of the values and compare it to a set chart.

If 20

They have: 46 posts

Joined: Aug 2001

Sorry ... clear as mud to me.

I will stick with JavaScript at this stage (Major time contraints). Later I can refine it and do it in PHP or Perl if this is better.

Annette

Busy's picture

He has: 6,151 posts

Joined: May 2001

here is a javascript quiz for you
http://members.home.net/garygrewal/jscripts/quiz.htm

its all explained pretty well for the non scripter to change to suit

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Dunno how much of help this will be but heres a script I made for a competition I entered.

http://schools.netguide.co.nz/lynfield/content.html?quizzes.html

It basically has all the answers stored in an array, and it loops through the user answers and compares to the correct answers. It also keeps a count of all the correct and wrong answers as it loops through. Then it passes all of the numbers as a string to a new window where another script dissects the string to create the results in a tabular format. Laughing out loud

The only problem is that it doesn't detect blank answers so the results go a little out of whack in that case. But hey, I had to make it in under 12 hours. Sticking out tongue
If you feel up to the challenge of fixing that and customising the code, you may. Wink

They have: 46 posts

Joined: Aug 2001

Hey guys thanks. I am looking at the code for each quiz example to unravel what I can use / change for my stuff ... but thought more info may help you to understand what I need.

Here is the mock up of the quiz I need the script for. There are no right or wrong answers - just need a total. Think of a Cosmo or Cleo magazine quiz ... Wink

http://www.seriousexcuses.com/quiz.html

Please ... no comments about lack of design on page (all that will change) or actual quiz itself ... Smiling

ALSO, can I please get you guys to check my code when I do it up. No doubt I will make a few mistakes, particularly given my ignorance. Would be most appreciated.

Thanks again.

Annette

... This is paying me back for having put off learning JavaScript Smiling ...

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Quote: Sorry ... clear as mud to me.

lol, sorry! Laughing out loud

You have your radio & check boxes, right?

And you get all your values, right?

And you know that a score of 1-10 is this answer, and 11-20 is this other answer and 21-30 is the other answer (for example, like a Cosmo quiz).

So when you get your value -- which is form.field[1].value + form.field[2].value, et cetera -- you just do a simple conditional statement.

IF (0 <= sum <= 10) {
var Answer = answera;
document.write whatever;
}

else if (11<= sum <=20) {
var Answer = answerb;
document.write whatever;
}

else {
var Answer = answerc;
document.write whatever;
}

This is just the logic, mind you, to illustrate. If the sum is between 0 and 10, then it's this answer, write it. If the sum is between 11 and 20, this it's this other answer, write it. If the sum isn't either of those, it must be this third option, write it.

You can even write it to a text area if you want people to be able to cut 'n' paste it into their pages, like in Bloggerville.

Hopefully it will help you see how to approach it. I haven't the time to work out the syntax, I apologize.

Smiling Suzanne

They have: 46 posts

Joined: Aug 2001

Sad

Okay, I have checked out the suggested scripts, poked and prodded, checked most of the previous posts here, checked out irt.org as previously recommended by Suzanne, and again checked other javascript sites

All seem to assume a basic knowledge of js.

I have tried a few variations ... each time I get either NaN or nothing or error on page.

I KNOW this is incredibly simple ...

... but I don't know how to get the page to add up the scores and display them.

Suzanne - I think you may be way ahead of me with your answer ... (but then again, it is most likely me who just doesn't understand ...).

At this stage I am happy with just a total number to pop up (ie q_1 value + q_2 value + ... = 26 with 26 being the number popping up for example), and the person using a text link to the explanation of results page.

Please don't laugh at my silly questions Laughing out loud -->

(1) Do I need a javascript message in the head tags?

(2) Do I need to change things as per Taff's previous post - adding *1 and so on - I'm sorry but it is all double dutch to me (not enough information given as to the solution for a js newbie like me).

(3) How do I write the onClick tag so it shows up the total amount?

(4) Where do put the "form.field[1].value + form.field[2].value" stuff that Suzanne talks about. In the onClick tag? Oh, and what is field[1]? Is that q_1? Or do I just write form.field[1]?

(5) What do I put in action=""? - I had the URL I want the results to show up on but this doesn't do anything. And as I said above, I'm happy with just a pop up answer at this stage.

(6) What is the difference between method="post"/="get"? And which one do I need? I'm interested in the difference as well as the answer to which one I need as I often use mailto forms and want to venture further with forms.

I would really like to spend more time on learning JavaScript but have spent all day today (and time previous to today) on just this quiz page and have way more to do before deadline arrives (Monday). I am at stage where I will gladly pay for a fully working script for this simple quiz. Anyone know where I can post to request a script? ... need it this weekend. Wink

Please help!

Annette :confused

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

No laughing here. Smiling

1) You're referring to this: <script>? If yes, then the answer to your questions is yes it is advisable. eg: <script language="text/javascript">
I forget what the new XML standard was but I think it varies from this. Anybody else know (too lazy to check Sticking out tongue)?

2) Yes, if you have problems with it.

3)   

4) See previous.

5) What you put in the action doesn't matter at this stage (using the code in this post) because it pops up in a message box.

6) GET uses the querystring when submitting info to another page/script so the involved data is visible in the browser address bar (appended to the address). POST hides that. Wink
GET eg: http://www.xyz.com?x=1&y=2

Additionally, can become .
That just clears the form when clicked.

Remove from your section.

They have: 46 posts

Joined: Aug 2001

A couple more things ...

(1) RE: You're referring to this: <script>? If yes, then the answer to your questions is yes it is advisable. eg: <script language="text/javascript">

Okay, but what do I put inside the script tags?

As far as my non-programming brain can work out, the onClick tag should provide the answer. But sorry, we are talking computers here, so can't possible be as simple as that ... as (3) shows.

(2) Yes, still getting NaN

(3) Thanks ... was looking for what the window.alert code ... so simple.

Going back to the solution Taff found in the post titled Assigning numerical values to radio buttons -
http://www.webmaster-forums.com/showthread.php?s=&threadid=16882

Solutions -
______________________________________________________

1) multiply each variable by 1 to make it numeric.

2) But then, it still wouldn't work. I suspect that a Radio button's value must be boolean. So I added a hidden field for each question (a1,a2,etc) and assigned the value to it using an onClick event

form.a1.value*1 + form.a2.value*1 + etc
_______________________________________________________

hidden field???? Does this mean I change my radio button tags to read -

and so on

instead of - ...

and then in the onClick tag put in

onclick="window.alert(quiz.1.value + quiz.2.value + quiz.3.value + ...

Anyway ... moot point because I did all this - nada ... then changed the quiz.1.value to form.1.value - nada ... No popup box pops up anymore either ...

So then changed it to form.1.value - nyet

then

form.1a.value - NO.

WHAT do I have to do to get this thing to work ...

Okay - the weather is getting to me as well. It is 9.00 pm and still VERY hot ... My brain is fried ... and I think the computer chip may well be too!

Please help ... Confused

Annette

taff's picture

They have: 956 posts

Joined: Jun 2001

Annette,

Here is the code I'm using (stripped of extraneous stuff and pared down to 3 questions)

*****************************

<script LANGUAGE="JavaScript">

</script>

*****************************

.....

They have: 46 posts

Joined: Aug 2001

From Confused --> Laughing out loud

It works!!!

Thank you to all who have helped. Taff, your code works for me ... and I understand what it means (well, sort of). This sure has been one learning experience for me. I found a tutorial I liked and helped explain things very simply - monosyllabic words and the like - and so at least I am not completly turned off by this scripting language.

And I want to muck about with forms a lot more too. Oh no, I hear you cringe ... she will be back. Yep, but hopefully not with an impossible deadline.

Thank you.

Annette

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.