• Resolved Anonymous User 17409631

    (@anonymized-17409631)


    Hi,
    I have a quick question as I couldn’t make a calculation work. I have in total 3 fields: age, gender, price. What I want to achieve is based on the gender (male / female) and the age a value (x) should be multipiled with the price (fieldname2).

    I have this formula but it doesn’t work:

    (function(){
    if(fieldname11=male + fieldname14=0) return (fieldname2*12*18.40);
    })();

    So if male and age (0) fieldname2 should be multiplied with 12 and 18.4

    Maybe there is a more elegant way to do this as I have 200 (male / female) and age (0-100) different values (x) in the above case 18.4 that I need to use. Can I say somewhere that the combination of 2 fields (age+ gender) has a specific value so that I don’t need to repeat the If clause 200 times?

    Hope someone can help with that. Thanks in advance!

    The page I need help with: [log in to see the link]

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

    (@codepeople)

    Hello @cfalco

    There are some issues in your equation:

    First, in javascript the operator for equality is double sign: “==”, because the sign “=” is used for assignment.

    Second, in javascript the textual values must be enclosed between single or double quotes.

    Third, in javascript the “AND” operator is represented with the double sign: &&

    So, the correct equation would be:

    
    (function(){
    if(fieldname11=='male' && fieldname14==0) return fieldname2*12*18.40;
    })();
    

    or using the “AND” operation distributed with the plugin:

    
    (function(){
    if(AND(fieldname11=='male', fieldname14==0)) return fieldname2*12*18.40;
    })();
    

    Best regards.

    Thread Starter Anonymous User 17409631

    (@anonymized-17409631)

    Many thanks @codepeople that’s amazing, you solved the riddle. Thanks as well for the hints for Javascript, I’m a complete newbie. Learned a lot!

    If there are different values for each combination of age and gender e.g. female age 41 is there an easy way to process that?

    What I had in mind is replicating the values 200x but there might be some issue with that regarding the load and complexity. But there are values attached to each combination:

    (function(){
    if(AND(fieldname11==’male’, fieldname14==0)) return fieldname2*12*18.40;
    if(AND(fieldname11==’male’, fieldname14==1)) return fieldname2*12*17.20;
    if(AND(fieldname11==’female’, fieldname14==0)) return fieldname2*12*20.85;
    if(AND(fieldname11==’female’, fieldname14==1)) return fieldname2*12*20.10;
    })();

    Plugin Author codepeople

    (@codepeople)

    Hello @cfalco

    You can reduce the complexity, at least in the execution, nesting the “if” conditional statements:

    
    (function(){
        if(fieldname11=='male')
        {
            if(fieldname14==0) return fieldname2*12*18.40;
            else return fieldname2*12*17.20;
        }
        else
        {
            if(fieldname14==0) return fieldname2*12*20.85;
            else return fieldname2*12*20.10;
        }
    })();
    

    Best regards.

    Thread Starter Anonymous User 17409631

    (@anonymized-17409631)

    Many thanks @codepeople super helpful. I tried however to extend the code above to cover more years:

    (function(){
        if(fieldname11=='male')
        {
            if(fieldname14==0) return fieldname2*12*18.400;
            else return fieldname2*12*18.464;
        }
        else
        {
            if(fieldname14==1) return fieldname2*12*18.389;
            else return fieldname2*12*18.456;
        }
    	else
        {
            if(fieldname14==2) return fieldname2*12*18.374;
            else return fieldname2*12*18.443;
        }
    	else
        {
            if(fieldname14==3) return fieldname2*12*18,357;
            else return fieldname2*12*18,430;
        }
    })();

    However this doesn’t work unfortunately. Do you know why?

    Also another related question: Is it possible to display another value in a separate field with the calculated value above?

    So my fields would be:

    age
    gender
    rent
    ——-
    calculated field (as above)
    + additional field (just displaying an additional value)

    Sorry for the many questions. Your reply really brought me forward!

    • This reply was modified 6 years, 10 months ago by Anonymous User 17409631.
    Plugin Author codepeople

    (@codepeople)

    Hello @cfalco

    Your use of the javascript conditional statement “if” is invalid (more information in the link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if…else). The correct would be:

    
    (function(){
    	var result;
        if(fieldname11=='male')
        {
            switch(fieldname14)
    		{
    			case 0:
    				result = fieldname2*12*18.400;
    			break;	
    			case 1:
    				result = fieldname2*12*18.389;
    			break;
    			case 2:
    				result = fieldname2*12*18.374;
    			break;
    			default:
    				result =  fieldname2*12*18.357;
    			break;
    		}
    	}
        else
    	{
    		switch(fieldname14)
    		{
    			case 0:
    				result = fieldname2*12*18.464;
    			break;	
    			case 1:
    				result = fieldname2*12*18.456;
    			break;
    			case 2:
    				result = fieldname2*12*18.443;
    			break;
    			default:
    				result =  fieldname2*12*18.430;
    			break;
    		}
    	}	
    	return result;
    })();
    

    You can nesting “if” conditional statements, I’ve decided to use the “switch” one to implement a more optimized equation(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch)

    If you want to use this result in another field, you are free to do it. You can use a calculated field in the equations of other calculated fields. The only restriction is take care of don’t generate an infinite loop (using the field A in the equation of field B, and the field B in the equation of field A).

    For example, assuming the previous field is the fieldname123, and you want to sum it to the value of the fieldname456 field.

    Note: the fields’ names are hypothetical.

    You simply should to insert another calculated field in the form with the equation:

    
    fieldname123+fieldname456
    

    and that’s all.
    Best regards.

    Thread Starter Anonymous User 17409631

    (@anonymized-17409631)

    You are amazing. Many thanks, it worked out very well.

    The thing with the additional field I need to try out as it is another fixed value and not something I can derive from the other fields.

    Can you also let me know how to limit the calculated field not to populate more than 2 decimal values? Currently it shows me numbers like 1.300,9767623

    Plugin Author codepeople

    (@codepeople)

    Hello @cfalco

    If you want to sum a fixed value to the result, for example, the number 12345, you simply should to edit the line of code in the equation:

    
    return result;
    

    as follows:

    
    return result+12345;
    

    Concerning to the decimal places. If you want to control the number of decimal places in the result, you should use the PREC operation: PREC(X,Y) returns the number X with Y decimal places.

    So assuming you want the result has only two decimal places, edit the previous line of code as follows:

    
    return PREC(result+12345, 2);
    

    and that’s all.
    Best regards.

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

The topic ‘IF Return Calculation’ is closed to new replies.