peterinwa
06-03-2003, 12:10 AM
In the very repetitive code, below, I pass through doing "stuff" for food, cal, pro, fat, car, fib and sod 3 times and I want to increase it to 10 times.
Is there a way to pass through a line of code n times and have it be food1, cal1, pro1, etc. one time and then food2, cal2, pro2, etc. the next? Something like food[n], cal[n], etc.? I've used that syntax for arrays but don't know what to do here.
Thanks, Peter
// Function to set personal food items (and validate text)
function setItem(n){
itemSelect=n;
temp=parent.image.document.forms["f2"].elements["food"+n].value;
if (n==1){food1=temp} if (n==2){food2=temp} if (n==3){food3=temp}
Vtext=temp;
temp=parent.image.document.forms["f2"].elements["cal"+n].value;
if (n==1){cal1=temp} if (n==2){cal2=temp} if (n==3){cal3=temp}
Vdata=temp+",";
temp=parent.image.document.forms["f2"].elements["pro"+n].value;
if (n==1){pro1=temp} if (n==2){pro2=temp} if (n==3){pro3=temp}
Vdata+=temp+",";
temp=parent.image.document.forms["f2"].elements["fat"+n].value;
if (n==1){fat1=temp} if (n==2){fat2=temp} if (n==3){fat3=temp}
Vdata+=temp+",";
temp=parent.image.document.forms["f2"].elements["car"+n].value;
if (n==1){car1=temp} if (n==2){car2=temp} if (n==3){car3=temp}
Vdata+=temp+",";
temp=parent.image.document.forms["f2"].elements["fib"+n].value;
if (n==1){fib1=temp} if (n==2){fib2=temp} if (n==3){fib3=temp}
Vdata+=temp+",";
temp=parent.image.document.forms["f2"].elements["sod"+n].value;
if (n==1){sod1=temp} if (n==2){sod2=temp} if (n==3){sod3=temp}
Vdata+=temp+",";
// Validate description
chkNumEqual=0;
if (food1.indexOf("=")!=-1){chkNumEqual++}
if (food2.indexOf("=")!=-1){chkNumEqual++}
if (food3.indexOf("=")!=-1){chkNumEqual++}
// Validate numbers
chkNum=0;
num=cal1; chkNumF(); num=pro1; chkNumF(); num=fat1; chkNumF(); num=car1; chkNumF(); num=fib1; chkNumF(); num=sod1; chkNumF();
num=cal2; chkNumF(); num=pro2; chkNumF(); num=fat2; chkNumF(); num=car2; chkNumF(); num=fib2; chkNumF(); num=sod2; chkNumF();
num=cal3; chkNumF(); num=pro3; chkNumF(); num=fat3; chkNumF(); num=car3; chkNumF(); num=fib3; chkNumF(); num=sod3; chkNumF();
if (chkNumEqual>0){chkNumEqualAlert()}
if (chkNum>0){chkNumAlert()}
if (chkNumEqual==0 & chkNum==0){saveItems()}}
Is there a way to pass through a line of code n times and have it be food1, cal1, pro1, etc. one time and then food2, cal2, pro2, etc. the next? Something like food[n], cal[n], etc.? I've used that syntax for arrays but don't know what to do here.
Thanks, Peter
// Function to set personal food items (and validate text)
function setItem(n){
itemSelect=n;
temp=parent.image.document.forms["f2"].elements["food"+n].value;
if (n==1){food1=temp} if (n==2){food2=temp} if (n==3){food3=temp}
Vtext=temp;
temp=parent.image.document.forms["f2"].elements["cal"+n].value;
if (n==1){cal1=temp} if (n==2){cal2=temp} if (n==3){cal3=temp}
Vdata=temp+",";
temp=parent.image.document.forms["f2"].elements["pro"+n].value;
if (n==1){pro1=temp} if (n==2){pro2=temp} if (n==3){pro3=temp}
Vdata+=temp+",";
temp=parent.image.document.forms["f2"].elements["fat"+n].value;
if (n==1){fat1=temp} if (n==2){fat2=temp} if (n==3){fat3=temp}
Vdata+=temp+",";
temp=parent.image.document.forms["f2"].elements["car"+n].value;
if (n==1){car1=temp} if (n==2){car2=temp} if (n==3){car3=temp}
Vdata+=temp+",";
temp=parent.image.document.forms["f2"].elements["fib"+n].value;
if (n==1){fib1=temp} if (n==2){fib2=temp} if (n==3){fib3=temp}
Vdata+=temp+",";
temp=parent.image.document.forms["f2"].elements["sod"+n].value;
if (n==1){sod1=temp} if (n==2){sod2=temp} if (n==3){sod3=temp}
Vdata+=temp+",";
// Validate description
chkNumEqual=0;
if (food1.indexOf("=")!=-1){chkNumEqual++}
if (food2.indexOf("=")!=-1){chkNumEqual++}
if (food3.indexOf("=")!=-1){chkNumEqual++}
// Validate numbers
chkNum=0;
num=cal1; chkNumF(); num=pro1; chkNumF(); num=fat1; chkNumF(); num=car1; chkNumF(); num=fib1; chkNumF(); num=sod1; chkNumF();
num=cal2; chkNumF(); num=pro2; chkNumF(); num=fat2; chkNumF(); num=car2; chkNumF(); num=fib2; chkNumF(); num=sod2; chkNumF();
num=cal3; chkNumF(); num=pro3; chkNumF(); num=fat3; chkNumF(); num=car3; chkNumF(); num=fib3; chkNumF(); num=sod3; chkNumF();
if (chkNumEqual>0){chkNumEqualAlert()}
if (chkNum>0){chkNumAlert()}
if (chkNumEqual==0 & chkNum==0){saveItems()}}