Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-05-2005, 06:41 AM   PM User | #1
GO ILLINI
Regular Coder

 
GO ILLINI's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 634
Thanks: 0
Thanked 7 Times in 7 Posts
GO ILLINI is an unknown quantity at this point
Quiz

well I am making a quiz. It uses radio buttons and a see my results button. I cant see anything wrong with the whole page but it doesent work...

Code:
<html>
<head>
<title></title>
<script>
var q1 = "0"
var q2 = "0"

function addup()  {
if (document.quiz.q1.yes.checked)  {
q1 + 1 = q1
}
}
results;

function results()  {
q1 + q2 = results
alert("you got " + results + " points")
}
</script>
</head>


<body onload="addup()">
<link REL="StyleSheet" TYPE="text/css" HREF="style.css"> 
<table width=100% class=mainquiz><td width=100% class=main2quiz>
<h1>The weird quiz</h1>
This page will tell you about how weird you are.<br><br>
<h2>Directions:</h2>
Check the one that is closest to your answer.<br><br>
<form name=quiz>
Question 1:<br>
If you saw gum stuck on the bottom of a table would you eat it?<br>
<input type=radio name="q1" value="Yes">Yes<br>
<input type=radio name="q1" value="No">No<br>
<input type="button" value="See my results!!" onclick="results()">
</form>
<!--#include virtual="/bottom.html" -->
can you see anything wrong with that? neither can I.
If you want to see the code in action go to http://adamsworld.name/weird.shtml

Sorry for makin you guys look through the whole code, i know i hate it when people do that but i can't find whats right and wrong!
thanks

GO ILLINI
GO ILLINI is offline   Reply With Quote
Old 07-05-2005, 07:47 AM   PM User | #2
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Couple things I see, you have a variable[results] named the same name as a your function.
Code:
function addup()  {
if (document.quiz.q1.yes.checked)  {
q1 + 1 = q1
}
}
results;
Are you meaning to call the results function there? Rename your variables accordingly, and this doesn't look right:
q1 + 1 = q1
I think you wanted:
q1++
Same with this:
q1 + q2 = results

If you test the page out in firefox it's really good with helping you debug your code.
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 07-05-2005, 08:31 AM   PM User | #3
jscheuer1
Regular Coder

 
Join Date: Mar 2005
Location: SE PA USA
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
jscheuer1 is an unknown quantity at this point
The value of an element is not used in finding it in the document in the way you have written the code. If you want a variable to be a number, don't use quotes around the value, that makes it a string. If you set your variables globally, changing the radio buttons' states will not change the value in the way expected. There is no reason to run any function onload for this nor, to have two functions. The previous poster's comments were also correct, mostly. Try this one:

Code:
<html>
<head>
<title></title>
<script type="text/javascript">
function addup()  {
var q1 = 0
var q2 = 0
if (document.forms.quiz.q1['yes1']){
if (document.forms.quiz.q1['yes1'].checked)
q1++
}
else if (document.getElementById){
if (document.getElementById('yes1').checked)
q1++
}
else
return;

var total = q1 + q2
alert("you got " + total+ " points")
}
</script>
</head>


<body>
<link REL="StyleSheet" TYPE="text/css" HREF="style.css"> 
<table width=100% class=mainquiz><td width=100% class=main2quiz>
<h1>The weird quiz</h1>
This page will tell you about how weird you are.<br><br>
<h2>Directions:</h2>
Check the one that is closest to your answer.<br><br>
<form name=quiz>
Question 1:<br>
If you saw gum stuck on the bottom of a table would you eat it?<br>
<input id="yes1" type=radio unchecked name="q1">Yes<br>
<input id="no1" type=radio unchecked name="q1">No<br>
<input type="button" value="See my results!!" onclick="addup()">
</form>
<!--#include virtual="/bottom.html" -->
jscheuer1 is offline   Reply With Quote
Old 07-05-2005, 07:42 PM   PM User | #4
GO ILLINI
Regular Coder

 
GO ILLINI's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 634
Thanks: 0
Thanked 7 Times in 7 Posts
GO ILLINI is an unknown quantity at this point
Thanks! I got it to work but I can't add another question! i dont get it

and is there a way to assign different point values to yes, no? because I want to have like 4 different choices!

And in the end It won't be a alert box that comes up, I will write a function to check the result var and it will take you to a different page depending on what number you came up with. so like:
points 1-5 you go to page 1
points 6-10 you go to page 2
and on and on

Last edited by GO ILLINI; 07-05-2005 at 08:54 PM..
GO ILLINI is offline   Reply With Quote
Old 07-05-2005, 10:23 PM   PM User | #5
jscheuer1
Regular Coder

 
Join Date: Mar 2005
Location: SE PA USA
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
jscheuer1 is an unknown quantity at this point
I don't mean to be disparaging however, given what seems to be your level of expertise in coding, I'm not surprised you are having problems expanding this script and its accompanying HTML markup. Tell you what, I'll make a copy of your post that I am responding to and paste it in as a comment to the demo file I made from my last post. When I get the chance I'll write a commented version showing how to include the features you've requested and post it, attach it or link it, back to this thread.
jscheuer1 is offline   Reply With Quote
Old 07-05-2005, 11:35 PM   PM User | #6
GO ILLINI
Regular Coder

 
GO ILLINI's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 634
Thanks: 0
Thanked 7 Times in 7 Posts
GO ILLINI is an unknown quantity at this point
Hey are you insulting my knowledge of javascript?
I know javascript but radio buttons are hell
GO ILLINI is offline   Reply With Quote
Old 07-05-2005, 11:45 PM   PM User | #7
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Their have been numerous quiz questions asked here, i'm sure you could find one allready made....in fact...http://www.codingforums.com/showthread.php?t=45757
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 07-06-2005, 12:19 AM   PM User | #8
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
Quote:
Originally Posted by GO ILLINI
Hey are you insulting my knowledge of javascript?
I know javascript but radio buttons are hell
Hmmm... Whom knew that the obvious was so tangible...
Thanks for the heads up... Much appreciated...

.....Willy
Willy Duitt is offline   Reply With Quote
Old 07-06-2005, 04:40 AM   PM User | #9
jscheuer1
Regular Coder

 
Join Date: Mar 2005
Location: SE PA USA
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
jscheuer1 is an unknown quantity at this point
Ah well, it was just that there was so much more amiss with your original code than just radio buttons. I thought I was helping you out. Anyways, here is the code, as promised:

Code:
<html>
<head>
<title>Weird Quiz</title>
<script type="text/javascript">
/*/////////////////////////////
* Quiz Evaluation Script © jscheuer1
* permission to use granted
* this credit must stay intact
/////////////////////////////*/

//Set to number of questions
var numQuest=3

////////////////Stop Editing///////////////

function addup()  {
var q, all, oft, som, nev, statement, total=0
questions=new Array();
for (i = 0; i < numQuest; i++)
questions[i]=0
assignQ=function(){
q="q"+(i+1)
all="all"+(i+1)
oft="oft"+(i+1)
som="som"+(i+1)
nev="nev"+(i+1)
}
if (document.forms.quiz.q1['yes1']){
for (i = 0; i < numQuest; i++){
assignQ()
if (document.forms.quiz[q][all].checked)
questions[i]=3
if (document.forms.quiz[q][oft].checked)
questions[i]=2
if (document.forms.quiz[q][som].checked)
questions[i]=1
}
}
else if (document.getElementById){
for (i = 0; i < numQuest; i++){
assignQ()
if (document.getElementById(all).checked)
questions[i]=3
if (document.getElementById(oft).checked)
questions[i]=2
if (document.getElementById(som).checked)
questions[i]=1
}
}
else
return;
for (i = 0; i < numQuest; i++)
total += questions[i]
/*/////////////////////////////////////////////////
Set totals and responses below.
If you wish to switch to a page instead of
showing the response on this page,
insert page name values for statement ex:
statement='weirdresponseone.htm'
and remove comment (//) from
'location.href=statement' line and comment
out the line above it.  Add more point ranges
and statements as needed.
///////////////////Edit Below to Suit/////////////*/
if (total<=4)
statement='We\'re all a little weird.'
else if (total<= 8)
statement='I <u>knew</u> there was something weird about you.'
else if (total<=12)
statement='Ungh! I\'m not hanging out with you!'
document.all? document.all.results.innerHTML=statement : document.getElementById('results').innerHTML=statement
//location.href=statement
/////////////////Stop Editing///////////////////
}
function clearR(){
document.all? document.all.results.innerHTML='&nbsp;' : document.getElementById('results').innerHTML='&nbsp;'
}
</script>
</head>
<link REL="StyleSheet" TYPE="text/css" HREF="style.css"> 
<body>
<h1>The weird quiz</h1>
This page will tell you about how weird you are.<br><br>
<h2>Directions:</h2>
Check the one that is closest to your answer.<br><br>
<form name="quiz">
<!--Edit and/or add questions below-->
<!--Be sure to follow the format shown-->
Question 1:<br>
If you saw gum stuck on the bottom of a table would you eat it?<br>
<input id="all1" type="radio" unchecked name="q1">Always<br>
<input id="oft1" type="radio" unchecked name="q1">Often<br>
<input id="som1" type="radio" unchecked name="q1">Sometimes<br>
<input id="nev1" type="radio" unchecked name="q1">Never<br>&nbsp;<br>
Question 2:<br>
If you saw gum stuck on your friend's nose would you eat it?<br>
<input id="all2" type="radio" unchecked name="q2">Always<br>
<input id="oft2" type="radio" unchecked name="q2">Often<br>
<input id="som2" type="radio" unchecked name="q2">Sometimes<br>
<input id="nev2" type="radio" unchecked name="q2">Never<br>&nbsp;<br>
Question 3:<br>
Do you sing while taking a shower?<br>
<input id="all3" type="radio" unchecked name="q3">Always<br>
<input id="oft3" type="radio" unchecked name="q3">Often<br>
<input id="som3" type="radio" unchecked name="q3">Sometimes<br>
<input id="nev3" type="radio" unchecked name="q3">Never<br>&nbsp;<br>
<!--End of Questions-->
<input type="button" value="Reset weird quiz" onclick="reset();clearR()">&nbsp;&nbsp;
<input type="button" value="See my results!!" onclick="addup()">&nbsp;&nbsp;<span id="results"></span>
</form></body></html>
jscheuer1 is offline   Reply With Quote
Old 07-08-2005, 03:19 AM   PM User | #10
askman
New Coder

 
Join Date: Jul 2005
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
askman is an unknown quantity at this point
hello thanks for the help i used this code:

Code:
<html>
<head>
<title>State Capitols Quiz</title>

<style type="text/css">
.chkans {
margin:10px 10px -5px 0;
} 

span.chkans {
position:relative;
top:5px
} 

* html span.chkans {
top:2px
} 
</style>

</head>
<body>
<h1>State Capitols Quiz</h1>
This page will tell what you know about state capitols.<br>
<h2>Directions:</h2>
Click on the correct answer.<br><br>

<script type="text/javascript">
/*/////////////////////////////
* Quiz Writing & Scoring Script
* © 2005 John Davenport Scheuer
* jscheuer1TAKEOUTCAPSFOREMAIL@comcast.net
* permission to use granted
* this credit must stay intact
/////////////////////////////*/

//Set to 0 for no answers given away
//Set to 1 to allow peeking
var allowPeeking=1

//Set to 0 for no Starting Over without refresh
//Set to 1 to allow Easy Do Overs
var allowDoOvers=1

questions=new Array();
//Set Questions and Answers Arrays Below.
//Questions come first.  Then come the answer pairs:
//Follow the right answer with "right", all wrong ones with ""
questions[0]=["What is the capitol of Pennsylvania?", "Harrisburg", "right", "Oswego", "", "Philadelphia", "", "Denver", ""]
questions[1]=["What is the capitol of New Jersey?", "Princeton", "", "North Orange", "", "Trenton", "right", "Labrador", ""]
questions[2]=["What is the capitol of Montana?", "Billings", "", "Helena", "right", "Shelby", "", "Tabasco", ""]
questions[3]=["What is the capitol of New York?", "Syracuse", "", "New York City", "", "Buffalo", "", "Albany", "right"]
questions[4]=["What is the capitol of Kansas?", "Atchison", "", "Topeka", "right", "Dodge City", "", "Manhattan", ""]
questions[5]=["Are we having fun yet?", "Yes", "right", "No", ""]

////////////////Stop Editing///////////////

for (i = 0; i < questions.length; i++){
for (j = 0; j < questions[i].length; j++){
if (questions[i][j]=="")
questions[i][j]=("w"+i)+j
if (questions[i][j]=="right")
questions[i][j]="right"+i
}
}
var ie=document.all
function showAnswer(el,ans){
ie? ie[el].innerHTML='The correct answer is: '+ ans : document.getElementById(el).innerHTML='The answer is: '+ ans
}

function addup()  {
var q, right, statement, total=0
quizQuests=new Array();

for (i = 0; i < questions.length; i++)
quizQuests[i]=0
if (document.forms.quiz.q0['right0']){
for (i = 0; i < questions.length; i++){
q="q"+i
right="right"+i
if (document.forms.quiz[q][right].checked)
quizQuests[i]=1
}
}
else if (document.getElementById){
for (i = 0; i < questions.length; i++){
right="right"+i
if (document.getElementById(right).checked)
quizQuests[i]=1
}
}
else
return;
for (i = 0; i < questions.length; i++)
total += quizQuests[i]
/*/////////////////////////////////////////////////
Set score response below.
///////////////////Edit Below to Suit/////////////*/
statement='You scored '+ total +' out of '+ questions.length +' correct, '+ Math.round(total/questions.length*100) +'%'
/////////////////Stop Editing///////////////////
ie? ie.results.innerHTML=statement : document.getElementById('results').innerHTML=statement
}
function clearR(){
ie? ie.results.innerHTML='' : document.getElementById('results').innerHTML=''
for (i = 0; i < questions.length; i++)
if (allowPeeking)
ie? ie["ans"+i].innerHTML='' : document.getElementById("ans"+i).innerHTML=''
window.scrollTo(0,0);
}
document.write('<hr><form name="quiz">')
var correct, answersString
for (i = 0; i < questions.length; i++){
answersString=''
for (k = 1; k < questions[i].length; k+=2)
answersString+='<input id="'+questions[i][(k+1)]+'" type="radio" unchecked name="q'+i+'"><label for="'+questions[i][(k+1)]+'">'+questions[i][k]+'</label><br>'
for (j = 0; j < questions[i].length; j++){
if (questions[i][j]=="right"+i)
correct=questions[i][j-1]
}
with (document){
write('Question '+(i+1)+':<br>')
write(questions[i][0]+'<br>')
write(answersString)
if (allowPeeking)
write('<input class="chkans" type="button" value="Check Answer" onclick="showAnswer(\'ans'+i+'\',\''+correct+'\')">&nbsp;<span id="ans'+i+'" class="chkans"></span><br>&nbsp;')
write('<br>')
}
}
with (document){
write('<hr><br>')
write('<input type="button" value="See Score" onclick="addup()">&nbsp;&nbsp;<span id="results"></span><br>&nbsp;<br>')
if (allowDoOvers)
write('<input type="button" value="Start Again" onclick="reset();clearR()">')
write('</form>')
}
</script>

</body></html>
it's good it can help: Thanks for that but what i need is whenever the "check answer" button was clicked it would say...

if correct: Correct. The state of New York is located in the East Coast.

if incorrect: Incorrect. The state of New York can't be located in the West
Coast.

The Correct answer is: The East Coast.

even if these 2 will appear in a "pop-up message" will do.

---------------------------------------------------------------------

that's all I need. Thanks For the Help

You guys are good! Thanks a LOT
__________________
my email is: boyaskman@yahoo.com
askman is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:51 AM.


Advertisement
Log in to turn off these ads.