...

View Full Version : Need Help with Syntax



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()}}

liorean
06-03-2003, 01:18 AM
I suggest you use something like an object literal/array literal combo for that instead. Here's an example based on your code (probably needs polishing and some modification):
function setItem(n){
var
oFoodData={
food:[],
cal:[],
pro:[],
fat:[],
car:[],
fib:[],
sod:[]
},
oForm=document.forms[f2],
aValues=[],
re=/=/,
i;
// I think you might want to declare
// oFoodData outside the function instead
for(i in oFoodData)
aValues[aValues.length]=oFoodData[i][n]=oForm.elements[i+n].value;
chkNumEqual=
0+
re.test(oFoodData.food[1])+
re.test(oFoodData.food[2])+
re.test(oFoodData.food[3]);
// I'm short-circuiting here.
// You might want to change this back.
chkNum=0;
n=3;
while(n>0){
for(i in oFoodData)
if(i!='food')
chkNumF(oFoodData[i][n]);
n--;
}
if(chkNumEqual>0)
chkNumEqualAlert();
if(chkNum>0)
chkNumAlert();
if(chkNumEqual+chkNum==0)
saveItems();
}

peterinwa
06-03-2003, 06:08 AM
Thanks! I'll have to study it to understand it but I'll have time soon.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum