• Resolved nuwebdesign

    (@nuwebdesign)


    I want to use the function IF ELSE but I’m not sure if I’m using it correctly. This is how I have it set up currently:

    function(){
    if(fieldname2 >= 1000000) return fieldname2;
    else return fieldname1;
    if(fieldname2 >= 300000) return ((fieldname2-300000)/1000) * 1.80;
    else return fieldname2;
    })()

    Do I need to have the else after each if or can I do a line of if’s and then at the end if none of those are valid rules, do the else and return the default value?

    Thanks!

    https://wordpress.org/plugins/calculated-fields-form/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter nuwebdesign

    (@nuwebdesign)

    There is a typo in the first line. Should say:

    if(fieldname2 >= 1000000) return fieldname2;
    else return fieldname2;

    I’ve tried both:

    function(){
    if(fieldname2 >= 1000000) return fieldname2;
    else return fieldname2;
    if(fieldname2 >= 300000) return ((fieldname2-300000)/1000) * 1.80;
    else return fieldname2;
    })()

    and:

    function(){
    if(fieldname2 >= 1000000) return fieldname2;
    if(fieldname2 >= 300000) return ((fieldname2-300000)/1000) * 1.80;
    else return fieldname2;
    })()

    both with no luck. Does this function need to go in the “Set Equation” box? Would it be easier using this format?

    (fieldname2>=300000) ? ((fieldname2-300000)/1000) * 1.80 : fieldname2

    The thing is I need it to check for multiple instances and return the value based on if it equal to or greater than multiple values and then perform an equation similar to the one listed above. I don’t know if I need to have them in multiple equation boxes and then all form into one so they validate right or if I can create a function in one box and have it spit out the right answer.

    Edit:

    I probably should of mentioned I’m trying to replicate some javascript that was built for the site I’m working on before I was handed lead on it. Here is the javascript snippet that I believe is the function that calculates the rates I’m trying to replicate:

    function ratetable1(L) { //refi
    var rate=265; //base rate
    // L = Liability
    switch (true) {
    case (L >= 1000000):
    rate = ‘0’;
    break;
    case (L > 400000):
    rate += Math.ceil((L-400000)/1000) * 1.50;
    case (L > 300000):
    if (rate>265) {rate+=180} else {rate += Math.ceil((L-300000)/1000) * 1.80;}
    case (L > 200000):
    if (rate>265) {rate+=200} else {rate += Math.ceil((L-200000)/1000) * 2.00;}
    case (L > 100000):
    if (rate>265) {rate+=210} else {rate += Math.ceil((L-100000)/1000) * 2.10;}
    case (L > 10000):
    if (rate>265) {rate+=198} else {rate += Math.ceil((L-10000)/1000) * 2.20;}
    }
    if (L==0) {rate=0;}
    return rate;
    }

    function ratetable2(L) {
    var rate=300; //base rate
    switch (true) {
    case (L >= 1000000):
    rate = ‘0’;
    break;
    case (L > 600000):
    rate += Math.ceil((L-600000)/1000) * 2.60;
    case (L > 300000):
    if (rate>300) {rate+=795} else {rate += Math.ceil((L-300000)/1000) * 2.65;}
    case (L > 200000):
    if (rate>300) {rate+=325} else {rate += Math.ceil((L-200000)/1000) * 3.25;}
    case (L > 100000):
    if (rate>300) {rate+=370} else {rate += Math.ceil((L-100000)/1000) * 3.70;}
    case (L > 50000):
    if (rate>300) {rate+=212.50} else {rate += Math.ceil((L-50000)/1000) * 4.25;}
    case (L > 10000):
    if (rate>300) {rate+=194} else {rate += Math.ceil((L-10000)/1000) * 4.85;}
    }
    if (L==0) {rate=0;}
    return rate;
    }

    The working calculator is found here. The full script is in the source code. Any help on this would be greatly appreciated.

    Thanks!

    Plugin Author codepeople

    (@codepeople)

    Hi,

    In the first case you should use the “(” symbol at beginning of code:

    (function(){
    if(fieldname2 >= 1000000) return fieldname2;
    if(fieldname2 >= 300000) return ((fieldname2-300000)/1000) * 1.80;
    else return fieldname2;
    })()

    You can use the same functions as the equations associated to the calculated fields, but using the field names of our plugin: fieldname1, fieldname2, fieldname3,…..

    And remember to use a function as an equation, you should use the structure:

    (function(){
    ….
    })()

    Best regards.

    Thread Starter nuwebdesign

    (@nuwebdesign)

    I have the following:

    (function(){

    if(fieldname2 > 10000) return 265;

    })();

    and it does not output anything. The field shows up blank. Same if I do:

    (function(){

    if(fieldname2 > 10000) return fieldname3;

    })();

    where fieldname3 equals 265. Both of these have the field appear blank when a value is inputed or not.

    Thread Starter nuwebdesign

    (@nuwebdesign)

    Just came through actually. Took awhile to render it for whatever reason. Is there a way to do a range? Like 100000 > fieldname >= 10000 in an IF function?

    Plugin Author codepeople

    (@codepeople)

    Hi,

    In the first question, be sure the fieldname2 has the correct value to return a result.

    Second, if you want check a range for the fieldname2, the correct is:

    if( 10000<=fieldname2 && fieldname2<100000 ) return ….

    Best regards.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Function IF ELSE’ is closed to new replies.