Click to See Complete Forum and Search --> : variable has no value ?
catchup
04-28-2003, 09:24 AM
I have some radio buttons in a form with the following names, e1,e2,e3,f1,f2, the user can select their langauage ability in both languages languages, however, i need only the summed value highest scoring language set (either english or french). to simplify things it would look like this:
English---read---write---speak
excellent-e1-----e2------e3
well-------e1-----e2------e3
poor------e1-----e2------e3
French-----read-----write-----speak
excellent----f1-------f2--------f3
well----------f1-------f2--------f3
poor---------f1-------f2--------f3
So i'm basically trying to add up the English values and then the French and see if the english selections are greateer than the french or vice versa, then i'm transposing this larger language set to a variable (lan)
The following only comes up with lan = 0, its like the code has teh correct syntax but the radio values are not being used to calculate the if then statement at the end, why does a1 & a2 variable have no value?
function Process () {
var itemchecked = false;
var data = "";
var a1 = a2 = lan = 0;
fr1 = document.f8;
x=1;
while(eval("fr1.e"+x)){
if(eval("fr1.e"+x+".checked")){
a1+=parseInt(eval("fr1.e"+x+".value"));
}
x++;
}
fr2 = document.f8;
y=1;
while(eval("fr2.f"+y)){
if(eval("fr2.f"+y+".checked")){
a2+=parseInt(eval("fr2.f"+y+".value"));
}
y++;
}
if (a1 > a2) {
lan = a1;
}else{
if (a2 > a1) {
lan = a2;
}else{
if (a2 = a1) {
lan = a1;
}
}
}
}
;)
gil davis
04-28-2003, 09:32 AM
In this part:fr2 = document.f8;
y=1;
while(eval("fr2.f"+x)){you are using x in the while statement, but you are incrementing y. x already has a value that is probably out of range. Therefore, a2 will not be updated.
catchup
04-28-2003, 09:56 AM
sorry, i print things out to follow code, its easier for me, i was retyping cut 'n pasting and i messed up, originally it is a 'y'. My problem remains, here:
function Process () {
var itemchecked = false;
var data = "";
var a1 = a2 = lan = 0;
fr1 = document.f8;
x=1;
while(eval("fr1.e"+x)){
if(eval("fr1.e"+x+".checked")){
a1+=parseInt(eval("fr1.e"+x+".value"));
}
x++;
}
fr2 = document.f8;
y=1;
while(eval("fr2.f"+y)){
if(eval("fr2.f"+y+".checked")){
a2+=parseInt(eval("fr2.f"+y+".value"));
}
y++;
}
if (a1 > a2) {
lan = a1
}else{
if (a2 > a1) {
lan = a2
}else{
if (a2 = a1) {
lan = a1
}
}
}
}
gil davis
04-28-2003, 10:27 AM
Do you understand the difference between =and==?if (a2 = a1) {
catchup
04-28-2003, 10:36 AM
I think i do, you use this = when to create a value and == as a statement,
i changed it and still the same result, it must be screwing up earlier in the code somewhere ...? any ideas
catchup
04-28-2003, 10:42 AM
maybe i'm better of writting it like so:
var first = document.f8.e1;
var second = document.f8.e2;
var third = document.f8.e3;
and when all the var are defined for each radio set then add them?
catchup
04-28-2003, 02:57 PM
I've been re-writing the code in different way and nothing seems to work cause lan value never seems to change from being zero.... this is dfriving me crazy,
this is what i have revised:
<html>
<head>
<script type="text/javascript"><!--
function Process()
{
var a1 = a2 = lan = 0;
var f = document.f8;
var x = 1;
var elem
while( elem = f.elements["e"+(x++)] )
{
if( elem.checked )
{
a1 += parseInt( elem.value, 9 );
}
}
x = 1;
while( elem = f.elements["f"+(x++)] )
{
if( elem.checked )
{
a2 += parseInt( elem.value, 9 );
}
}
lan = ( a1 >= a2 ) ? a1 : a2;
document.write (lan)
}
//--></script>
</head>
<body>
<form name="f8">
<p>English</p>
<p>e1<input type="radio" value="10" name="e1">e2<input type="radio" name="e2" value="10">e3<input type="radio" name="e3" value="10"></p>
<p>e1<input type="radio" name="e1" value="5">e2<input type="radio" value="5" name="e2">e3<input type="radio" name="e3" value="5"></p>
<p>e1<input type="radio" name="e1" value="2">e2<input type="radio" name="e2" value="2">e3<input type="radio" value="2" name="e3"></p>
<p>French</p>
<p>f1<input type="radio" value="10" name="f1">f2<input type="radio" name="f2" value="10">f3<input type="radio" name="f3" value="10"></p>
<p>f1<input type="radio" name="f1" value="5">f2<input type="radio" value="5" name="f2">f3<input type="radio" name="f3" value="5"></p>
<p>f1<input type="radio" name="f1" value="1">f2<input type="radio" name="f2" value="1">f3<input type="radio" value="1" name="f3"></p>
<p><input type="Submit" value="Next" onclick="Process();"/></p>
</form>
</body>
</html>
gil davis
04-28-2003, 03:27 PM
You weren't deep enough. You have an array of radio buttons:
document.f8.e1[n]; // where n=0 to 2
document.f8.e2[n]; // where n=0 to 2
document.f8.e3[n]; // where n=0 to 2
document.f8.f1[n]; // where n=0 to 2
document.f8.f2[n]; // where n=0 to 2
document.f8.f3[n]; // where n=0 to 2
and you aren't stepping through them correctlyfunction Process() {
var a1 = a2 = lan = 0;
var f = document.f8;
var x = 1;
var elem;
while (elem = f["e" + x]) {
for (var i=0; i<elem.length; i++)
{if (elem[i].checked)
{a1 += parseInt(elem[i].value, 10);}
}
x++;
}
x = 1;
while (elem = f["f" + x]) {
for (var i=0; i<elem.length; i++)
{if( elem.checked )
{a2 += parseInt(elem[i].value, 10);}
}
x++;
}
lan = ( a1 >= a2 ) ? a1 : a2;
alert(lan);
}