• Resolved Simba

    (@simbalion)


    For some reason this isn’t calculating, am I doing something wrong with the js syntax, or is it something else? Also, is there any sort of limitation with the size of formulas, and is the outermost () of (function(){}); always necessary or only on the first function()?

    (function(){
    var year = fieldname2, exemptions = fieldname49, taxable = fieldname33;
    	if(exemptions > 1){
    		function(){
    		var taxpct = 1,
    		b1 = 12150, b1p = 0.1,
    		b2 = 46250, b2p = 0.15,
    		b3 = 119400, b3p = 0.25,
    		b4 = 193350, b4p = 0.28,
    		b5 = 379150, b5p = 0.33,
    		b6p = 0.35,
    		maxtax1 = b1*b1p,
    		maxtax2 = (b2-b1)*b2p,
    		maxtax3 = (b3-b2)*b3p,
    		maxtax4 = (b4-b3)*b4p,
    		maxtax5 = (b5-b4)*b5p,
    		maxtax6 = (b6-b5)*b6p,
    		if(taxable < b1){var taxpct= b1p, bracketmaximum= b1, max= maxtax1;}
    		if(taxable >= b1 && taxable < b2){var taxpct = b2p, bracketmaximum = b2, max = maxtax2;}
    		if(taxable >= b2 && taxable < b3){var taxpct = b3p, bracketmaximum = b3, max = maxtax3;}
    		if(taxable >= b3 && taxable < b4){var taxpct = b4p, bracketmaximum = b4, max = maxtax4;}
    		if(taxable >= b4 && taxable < b5){var taxpct = b5p, bracketmaximum = b5, max = maxtax5;}
    		if(taxable >= b5 && taxable < b6){var taxpct = b6p, bracketmaximum = b6, max = maxtax6;}
    		var difference = (bracketmaximum - taxable), bracktax = (difference * taxpct), final = (bracktax + max);
    		return prec(final);
    		};
    	}
    	else {
    		function(){
    		var taxpct = 1,
    		b1 = 8500, b1p = 0.1,
    		b2 = 34500, b2p = 0.15,
    		b3 = 83600, b3p = 0.25,
    		b4 = 174400, b4p = 0.28,
    		b5 = 379150, b5p = 0.33,
    		b6p = 0.35,
    		maxtax1 = b1*b1p,
    		maxtax2 = (b2-b1)*b2p,
    		maxtax3 = (b3-b2)*b3p,
    		maxtax4 = (b4-b3)*b4p,
    		maxtax5 = (b5-b4)*b5p,
    		maxtax6 = (b6-b5)*b6p,
    		if(taxable < b1){var taxpct= b1p, bracketmaximum= b1, max= maxtax1;}
    		if(taxable >= b1 && taxable < b2){var taxpct = b2p, bracketmaximum = b2, max = maxtax2;}
    		if(taxable >= b2 && taxable < b3){var taxpct = b3p, bracketmaximum = b3, max = maxtax3;}
    		if(taxable >= b3 && taxable < b4){var taxpct = b4p, bracketmaximum = b4, max = maxtax4;}
    		if(taxable >= b4 && taxable < b5){var taxpct = b5p, bracketmaximum = b5, max = maxtax5;}
    		if(taxable >= b5 && taxable < b6){var taxpct = b6p, bracketmaximum = b6, max = maxtax6;}
    		var difference = (bracketmaximum - taxable), bracktax = (difference * taxpct), final = (bracktax + max);
    		return prec(final);
    		};
    	};
    }();

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

Viewing 1 replies (of 1 total)
  • Plugin Author codepeople

    (@codepeople)

    Hi,

    The sintax of your equation is wrong, you only need the outer most (function(){})() structure. Inside a function you can define other functions but the structure to use would be different. In the code below I’ve not checked the name of variables or the values, only the structure of the function, please, pay attention, I’ve replaced the comma symbol (,), by the semicolon (;), in the last variable definition, previous to the “if” statements. I really guess that your equation can be implemented in a simpler way:

    (function(){
    var year = fieldname2, exemptions = fieldname49, taxable = fieldname33;
    	if(exemptions > 1){
    		var taxpct = 1,
    		b1 = 12150, b1p = 0.1,
    		b2 = 46250, b2p = 0.15,
    		b3 = 119400, b3p = 0.25,
    		b4 = 193350, b4p = 0.28,
    		b5 = 379150, b5p = 0.33,
    		b6p = 0.35,
    		maxtax1 = b1*b1p,
    		maxtax2 = (b2-b1)*b2p,
    		maxtax3 = (b3-b2)*b3p,
    		maxtax4 = (b4-b3)*b4p,
    		maxtax5 = (b5-b4)*b5p,
    		maxtax6 = (b6-b5)*b6p;
    
    		if(taxable < b1){var taxpct= b1p, bracketmaximum= b1, max= maxtax1;}
    		if(taxable >= b1 && taxable < b2){var taxpct = b2p, bracketmaximum = b2, max = maxtax2;}
    		if(taxable >= b2 && taxable < b3){var taxpct = b3p, bracketmaximum = b3, max = maxtax3;}
    		if(taxable >= b3 && taxable < b4){var taxpct = b4p, bracketmaximum = b4, max = maxtax4;}
    		if(taxable >= b4 && taxable < b5){var taxpct = b5p, bracketmaximum = b5, max = maxtax5;}
    		if(taxable >= b5 && taxable < b6){var taxpct = b6p, bracketmaximum = b6, max = maxtax6;}
    		var difference = (bracketmaximum - taxable), bracktax = (difference * taxpct), final = (bracktax + max);
    		return prec(final);
    	}
    	else {
    		var taxpct = 1,
    		b1 = 8500, b1p = 0.1,
    		b2 = 34500, b2p = 0.15,
    		b3 = 83600, b3p = 0.25,
    		b4 = 174400, b4p = 0.28,
    		b5 = 379150, b5p = 0.33,
    		b6p = 0.35,
    		maxtax1 = b1*b1p,
    		maxtax2 = (b2-b1)*b2p,
    		maxtax3 = (b3-b2)*b3p,
    		maxtax4 = (b4-b3)*b4p,
    		maxtax5 = (b5-b4)*b5p,
    		maxtax6 = (b6-b5)*b6p;
    
    		if(taxable < b1){var taxpct= b1p, bracketmaximum= b1, max= maxtax1;}
    		if(taxable >= b1 && taxable < b2){var taxpct = b2p, bracketmaximum = b2, max = maxtax2;}
    		if(taxable >= b2 && taxable < b3){var taxpct = b3p, bracketmaximum = b3, max = maxtax3;}
    		if(taxable >= b3 && taxable < b4){var taxpct = b4p, bracketmaximum = b4, max = maxtax4;}
    		if(taxable >= b4 && taxable < b5){var taxpct = b5p, bracketmaximum = b5, max = maxtax5;}
    		if(taxable >= b5 && taxable < b6){var taxpct = b6p, bracketmaximum = b6, max = maxtax6;}
    		var difference = (bracketmaximum - taxable), bracktax = (difference * taxpct), final = (bracktax + max);
    		return prec(final);
    	}
    }()

    I really prefer to use the following equation, because in the original equation your are duplicating a lot of code:

    (function(){
        var year = fieldname2, exemptions = fieldname49, taxable = fieldname33, taxpct = 1, b1p = 0.1, b2p = 0.15, b3p = 0.25, b4p = 0.28, b5p = 0.33, b6p = 0.35;
    	var b1 = 8500, b2 = 34500, b3 = 83600, b4 = 174400, b5 = 379150;
    	if(exemptions > 1)
    	{
    		b1 = 12150;
    		b2 = 46250;
    		b3 = 119400;
    		b4 = 193350;
    		b5 = 379150;
    	}
    
    	var maxtax1 = b1*b1p,
    	maxtax2 = (b2-b1)*b2p,
    	maxtax3 = (b3-b2)*b3p,
    	maxtax4 = (b4-b3)*b4p,
    	maxtax5 = (b5-b4)*b5p,
    	maxtax6 = (b6-b5)*b6p;
    
    	if(taxable < b1){var taxpct= b1p, bracketmaximum= b1, max= maxtax1;}
    	else if( taxable < b2){var taxpct = b2p, bracketmaximum = b2, max = maxtax2;}
    	else if( taxable < b3){var taxpct = b3p, bracketmaximum = b3, max = maxtax3;}
    	else if( taxable < b4){var taxpct = b4p, bracketmaximum = b4, max = maxtax4;}
    	else if( taxable < b5){var taxpct = b5p, bracketmaximum = b5, max = maxtax5;}
    	esle if( taxable < b6){var taxpct = b6p, bracketmaximum = b6, max = maxtax6;}
    
    	var difference = (bracketmaximum - taxable), bracktax = (difference * taxpct), final = (bracktax + max);
    	return prec(final);
    
    }()

    Pay attention to the conditional statements in the original and the modified equation, you are not taking into account if the value of “taxable” is bigger than or equal to “b6”.

    Best regards.

Viewing 1 replies (of 1 total)

The topic ‘What's wrong with my formula?’ is closed to new replies.