Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global var

Status
Not open for further replies.
Nov 6, 2002
3
US
I'm making a test using forms and I type up the javascript that will keep track of the score. Everytime a person submits the score will add up but when they resubmit the score does not start at zero. It just adds to the old score. I want to make my score var a global variable so I can use the score for another function this is what I got so far:

<script type=&quot;text/javascript&quot;>
var score = 0;

function validate()
{

x1=document.myForm
a1=x1.aa.value
a2=x1.ab.value
a3=x1.ac.value
a4=x1.ad.value
a5=x1.ae.value
a6=x1.af.value
a7=x1.ag.value
a8=x1.ah.value

if (a1 == &quot;&quot;)
{
alert(&quot;Please fill in something in Question 1&quot;);
}
else
{
if (a1 == &quot;pixels&quot; || a1 == &quot;pixel&quot; || a1 == &quot;Pixel&quot; || a1 == &quot;Pixels&quot; || a1 == &quot;PIXEL&quot; || a1 == &quot;PIXELS&quot;)
{
alert(a1 + &quot; is the right answer&quot;);
score = score + 1;
}
else
{
alert(a1 + &quot; is NOT the right answer for Question 1&quot;);
}
}
if (a2 == &quot;&quot;)
{
alert(&quot;Please fill in something in Question 2&quot;);
}
else
{
if (a2 == &quot;Debug&quot; || a2 == &quot;DEBUG&quot; || a2 == &quot;debug&quot; || a2 == &quot;Debugging&quot; || a2 == &quot;DEBUGGING&quot; || a2 == &quot;debugging&quot;)
{
alert(a2 + &quot; is the right answer&quot;);
score = score + 1;
}
else
{
alert(a2 + &quot; is NOT the right answer for Question 2&quot;);
}
}
if (a3 == &quot;&quot;)
{
alert(&quot;Please fill in something in Question 3&quot;);
}
else
{
if (a3 == &quot;byte&quot; || a3 == &quot;Byte&quot; || a3 == &quot;BYTE&quot;)
{
alert(a3 + &quot; is the right answer&quot;);
score = score + 1;
}
else
{
alert(a3 + &quot; is NOT the right answer for Question 3&quot;);
}
}
{
if (a4 == &quot;ONE&quot; || a4 == &quot;one&quot; || a4 == &quot;One&quot;)
{
alert(a4 + &quot; is the right answer&quot;);
score = score + 1;
}
else
{
alert(a4 + &quot; is NOT the right answer for Question 4&quot;);
}
}
{
if (a5 == &quot;UNIX&quot; || a5 == &quot;Unix&quot; || a5 == &quot;Unix&quot;)
{
alert(a5 + &quot; is the right answer&quot;);
score = score + 1;
}
else
{
alert(a5 + &quot; is NOT the right answer for Question 5&quot;);
}
}
{
if (a6 == &quot;programs&quot; || a6 == &quot;program&quot; || a6 == &quot;Programs&quot; || a6 == &quot;PROGRAMS&quot; || a6 == &quot;PROGRAM&quot; || a6 == &quot;Program&quot; )
{
alert(a6 + &quot; is the right answer&quot;);
score = score + 1;
}
else
{
alert(a6 + &quot; is NOT the right answer for Question 6&quot;);
}
}
{
if (a7 == &quot;virus&quot; || a7 == &quot;VIRUS&quot; || a7 == &quot;Virus&quot;)
{
alert(a7 + &quot; is the right answer&quot;);
score = score + 1;
}
else
{
alert(a7 + &quot; is NOT the right answer for Question 7&quot;);
}
}
{
if (a8 == &quot;no&quot; || a8 == &quot;NO&quot; || a8 == &quot;No&quot;)
{
alert(a8 + &quot; is the right answer&quot;);
score = score + 1;
}
else
{
alert(a8 + &quot; is NOT the right answer for Question 8&quot;);
}
}
if (score > 0)
{
alert(&quot;Your total score is: &quot; + score);
}
if (score > 6)
{
document.bgColor=&quot;ff99ff&quot;;
}
totalscore()
return false;
}
}
 
when you say submit, do you mean the page is actually submitting the form via GET or POST? or is it just running the function validate()? if it's the latter, the method you're using should work fine, unless you're resetting &quot;score&quot; somewhere else.

i also noticed that you could simplify some of your text comparisons like so:

from
if (a1 == &quot;pixels&quot; || a1 == &quot;pixel&quot; || a1 == &quot;Pixel&quot; || a1 == &quot;Pixels&quot; || a1 == &quot;PIXEL&quot; || a1 == &quot;PIXELS&quot;)


to
if (a1.toLowerCase() == &quot;pixels&quot; || a1.toLowerCase() == &quot;pixel&quot;)
=========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top