PDA

View Full Version : Another Newb Question


Pace
10-27-2002, 06:23 PM
I'm trying to make a little quiz for a website i'm making for my school programming class. I can't get it to count the number right and wrong. here is what i have:

<html>
<head>

<script language="JavaScript">
<!--
function calc_result() {
oAnswer.innerHTML="You got "
+ parseInt(quiz1.q1.value)
+ parseInt(quiz1.q2.value)
+ parseInt(quiz1.q3.value)
+ parseInt(quiz1.q4.value)
+ parseInt(quiz1.q5.value)
+ " out of 5 correct.

";
}


-->
</script>

</head>

<body>

<form name="quiz1">

How many teams move on past the first round in the World Cup?


<input type="radio" name="q1" value="0">32

<input type="radio" name="q1" value="1">16

<input type="radio" name="q1" value="0">8

<input type="radio" name="q1" value="0">4




Which country won the World Cup 2002?


<input type="radio" name="q2" value="0">France

<input type="radio" name="q2" value="0">Germany

<input type="radio" name="q2" value="1">Brazil

<input type="radio" name="q2" value="0">England




Who scored the most goals in the World Cup 2002?


<input type="radio" name="q3" value="0">Landon Danavon

<input type="radio" name="q3" value="0">David Beckham

<input type="radio" name="q3" value="0">Ranaldo

<input type="radio" name="q3" value="0">Ravaldo




How many goals did he score?


<input type="radio" name="q4" value="0">6

<input type="radio" name="q4" value="0">7

<input type="radio" name="q4" value="1">8

<input type="radio" name="q4" value="0">9




How many total goals were scored in the World Cup 2002?


<input type="radio" name="q5" value="0">143

<input type="radio" name="q5" value="0">152

<input type="radio" name="q5" value="0">157

<input type="radio" name="q5" value="0">161




<input type="reset" value="Reset Answers">
<input type="button" value="Submit Answers" onclick="calc_result();">




<span id="oAnswer"></span>

</body>
</html>

here is what happens:

http://www.isaboo.net/pace/quiz.JPG

help me :o

-Pace

gish
10-27-2002, 07:23 PM
NaN = Not a Number

you are trying to minipulate a string in this case

Pace
10-27-2002, 09:19 PM
how do i make it a number? i put the value as a number and a did that parseInt thing.

-Pace

inkedmn
10-27-2002, 09:28 PM
k, screw-up thread deleted

gish
10-28-2002, 01:49 PM
Originally posted by Pace
how do i make it a number? i put the value as a number and a did that parseInt thing.

-Pace

wel....i will take a closer look at work...if I get a "free" min......

madMoney
10-28-2002, 09:11 PM
javascript can't access the value property of a radio button like server side scripts can - it doesn't determine which one is checked and return its value, it just always returns "undefined."

The way you have to do it is loop through all elements with that name, find the index of the one that is checked, and access its value in the following way: formName.radioButtonName[index].value

Try:


theTotal = 0;

for (i = 0;i<quiz1.q1.length;i++) {
if(quiz1.q1[i].checked && quiz1.q1[i].value==1) { theTotal++; }
}

for (i = 0;i<quiz1.q2.length;i++) {
if(quiz1.q2[i].checked && quiz1.q2[i].value==1) { theTotal++; }
}

for (i = 0;i<quiz1.q3.length;i++) {
if(quiz1.q3[i].checked && quiz1.q3[i].value==1) { theTotal++; }
}

for (i = 0;i<quiz1.q4.length;i++) {
if(quiz1.q4[i].checked && quiz1.q4[i].value==1) { theTotal++; }
}

for (i = 0;i<quiz1.q5.length;i++) {
if(quiz1.q5[i].checked && quiz1.q5[i].value==1) { theTotal++; }
}


oAnswer.innerHTML="You got " + theTotal + " out of 5 correct.

";


or, if you know they all have the same number of options, you can replace all those loops with just


for (i = 0;i<quiz1.q1.length;i++) {
if(quiz1.q1[i].checked && quiz1.q1[i].value==1) { theTotal++; }
if(quiz1.q2[i].checked && quiz1.q2[i].value==1) { theTotal++; }
if(quiz1.q3[i].checked && quiz1.q3[i].value==1) { theTotal++; }
if(quiz1.q4[i].checked && quiz1.q4[i].value==1) { theTotal++; }
if(quiz1.q5[i].checked && quiz1.q5[i].value==1) { theTotal++; }
}


ps, i have no idea if this is cross-browser compatible :)

Pace
10-28-2002, 10:03 PM
:D it worked!
thx :>

I bet my teacher is gonna ask how i got it to work. I'll give you credit, but if you could just walk through what is going on, it might help. What is ++?

-Pace

scanez
10-28-2002, 10:06 PM
num++ is equivalent to num = num + 1
num -- is equivalent to num = num - 1
num += blah is equivalent to num = num + blah

I think you can figure out num -=, num *=, etc. from there ;)

madMoney
10-28-2002, 11:53 PM
Sure I'll walk ya thru it.

theTotal = 0;

I think you can figure this part out :)

for (i = 0;i<quiz1.q1.length;i++) {
if(quiz1.q1[i].checked && quiz1.q1[i].value==1) { theTotal++; }
}

the for-loop is looping from 0 to the number of objects in the form that have the name q1. Javascript is treating q1 as an array of radio buttons, so the '.length' property is the number of objects in that array.

I am using "i" as the index variable (or counter) of the loop, so inside the loop, i check to see if the "i"th radio button is checked, and if it is checked, if it's the correct answer. If both of these things are true, like scanez said, ++ adds 1 to the total.

I then do this little bit of code for each set of buttons on the page (q1 through q5)

oAnswer.innerHTML="You got " + theTotal + " out of 5 correct.

";

I think this was your code originally :)

hope this makes sense

Pace
10-29-2002, 04:02 PM
i get it now. thanks for walking through it :]

-Pace