Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need Help with Syntax

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

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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):
    Code:
    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();
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks! I'll have to study it to understand it but I'll have time soon.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •