• Resolved hheyhey568

    (@hheyhey568)


    Hello team,

    I have do while loop. But when one of the Calculated variable turns negative , the whole loop freezes the page and I need to close the WordPress and re open it.

    Is there a way to exit the loop when one variable gets negative and give the user warning message to increase the value ?

    Regards,

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @hheyhey568

    The do/while loops in javascript have no problems by themselves. The problem is the way you use it. The browser evaluates the do/while loop while the condition is true.

    Unfortunately, you do not include the equation. But assuming it uses two fields, fieldname1 and fieldname2, and you want to evaluate the loop only if the fields values are greater than or equal to zero. The equation structure would be similar to:

    (function(){
        if(AND(0<=fieldname1, 0<=fieldname2)) {
    
            /* YOUR LOOP HERE */  
    
        } else {
             alert('The fields values are invalid');
        }
    })()

    Best regards.

    Thread Starter hheyhey568

    (@hheyhey568)

    Hello Team,
    It is working but when it gives alert , it keeps giving alert.

    I feel the loop keeping running and continuously giving alert. It is not allowing to change any input variable.

    Is there a way , by which we can give alert and if user press ok then the whole loop reset and gives user permission to update the input variables?

    Please note that it is calculated variable inside the loop which gets negative.

    thanks

    • This reply was modified 1 year, 6 months ago by hheyhey568.
    Plugin Author codepeople

    (@codepeople)

    Hello @hheyhey568

    Please, include your equation’s code to check it.

    Best regards.

    Thread Starter hheyhey568

    (@hheyhey568)

    Hello Team,

    Following is the code.

    When OP becomes negative , the whole page freeze.

    Pls help to find a way which can kill the loop from running once OP becomes negative and gives user alert to check the inputs.

    Thanks

    (function(){

    var DA,OP,OD,OF,OV,AV,AD,Number,Factor,Fcalculated,Result;

    DA=0.001;

    do{

    OP=(fieldname83+101.325/100)-DA;

    OD=OP*100*fieldname64/8.314/(fieldname79+273.15);

    OF=fieldname80/OD;

    OV=(OF/3600)/(Math.PI/4*pow((fieldname14*25.4/1000),2));

    AV=(OV+fieldname1)/2;
    AD=(OD+fieldname65)/2;

    Number=((fieldname14*2.54)*(AV*100)*(AD/1000))/(fieldname84/100);

    if(Number<=2320) Fcalculated=64/Number;

    if(AND(fieldname14,fieldname5,Number,0<=fieldname63,fieldname82))
    {
    var DI=fieldname14*25.4/1000;
    var RF=fieldname82/1000;
    var NNumner=Number;
    var Factor=0.001,LHS,RHS;

    do{
    LHS=1/sqrt(Factor);
    RHS=-2*LOGAB(RF/(3.7*DI)+2.51/(NNumner*sqrt(Factor)),10);
    Factor=Factor+0.0001;
    }while(ABS(LHS-RHS)>0.05)

    Fcalculated=PREC(Factor,5);

    }

    Result=PREC(((Fcalculated*fieldname81*pow(AV,2)*AD)/(2*fieldname14*25.4/1000))/(1000*100),3);

    DA=DA+0.001;

    }while(ABS(DA-Result)>0.05)

    getField(70).setVal(PREC(OD,4));
    getField(69).setVal(PREC(OV,5));
    getField(10).setVal(PREC(Number,2));
    getField(11).setVal(PREC(Fcalculated,5));
    getField(71).setVal(PREC(OF,5));
    getField(85).setVal(PREC(OP-101.325/100,5));

    if(OR(OD<=0,(OP-101.325/100)<=0))
    {
    IGNOREFIELD(70);
    IGNOREFIELD(69);
    IGNOREFIELD(10);
    IGNOREFIELD(11);
    IGNOREFIELD(71);
    IGNOREFIELD(57);
    IGNOREFIELD(62);

    alert(‘Check Inputs’);

    return “Check Inputs”;
    }
    else
    {
    ACTIVATEFIELD(70);
    ACTIVATEFIELD(69);
    ACTIVATEFIELD(10);
    ACTIVATEFIELD(11);
    ACTIVATEFIELD(71);
    ACTIVATEFIELD(57);
    ACTIVATEFIELD(62);
    return Result;

    }

    })();

    Plugin Author codepeople

    (@codepeople)

    Hello @hheyhey568

    I’m sorry, I cannot tell you how to implement the equation because I don’t know your business specifications. However, there are some points you must keep in mind when you implement the equations.

    – First point the do/while stop conditions.

    Your equation includes two do/while loops:

    do { } while (ABS(LHS - RHS) > 0.05)

    and

    do { } while (ABS(DA - Result) > 0.05)

    If the value of any of these operations grows over 0.05, the do/while will never stop.

    About the ACTIVATEFIELD and IGNOREFIELD operations, the values of the ignore fields become zero, and values of the activate fields are their original ones. So, when you call the IGNOREFIELD or ACTIVATEFIELD operations, the plugin evaluates every equation that uses these fields as operands. The same happens when you assign fields values with the getField(X).setVal(Y) method. You must take special care not to generate an endless loop.

    Best regards.

    Thread Starter hheyhey568

    (@hheyhey568)

    Hello Thanks for your response.

    I know that it will go into endless loop but is there any way to just give alert and exit the endless loop so that it gives the user chance to update to correct inputs?

    Thank for help.

    Plugin Author codepeople

    (@codepeople)

    Hello @hheyhey568

    The code doesn’t know when it is in an endless loop. You must tell the code the stop condition.

    For example, assuming you have a loop:

    
    (function(){
    var b = 5, a = 10;
    do{
    b = b - 1;
    a = a + 1;
    }while( b < a);
    return a;
    })()
    

    This equation evidently has an endless loop, and there is no way the code knows by itself that it is in an endless loop. But as I know the business logic to implement, I can decide to stop the loop if it is evaluated 50 times. In this case, I must include another condition in the “while”:

    
    (function(){
    var b = 5, a = 10, c = 0;
    do{
    b = b - 1;
    a = a + 1;
    c = c+1
    }while( b < a && c < 50);
    return a;
    })()
    

    I included another condition because I know how to proceed in a specific situation.

    Best regards.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Do while loop’ is closed to new replies.