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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need help understanding how a piece of code processes a formula...

    Hello, haven't played with coding since early 2000's, and while I can normally figure stuff out well enough for a basic understanding, I'm having a lot of trouble understanding this code well enough to fix it. Using Joomla/Virtuemart, I had a contractor set up so that a formula entered in a field in the backend would process drop down input from the front end to modify a value.

    The backend formula format is:

    {Product_Age}+{1}*{Product_Value}

    Everything has to be in the curly brackets, and only the basic 4 operators are supported. The above calculates as expected. The problem I'm having is that if I try to put a number first, like:

    {10}-{Product_Age}/{10}*{Product_Value}

    it breaks something, and will only display: NaN

    However, if I put extra steps in the middle it will still work, so I know it's not the number of steps.

    I've been going through the pages he modified, and I think I've found the code that processes the formula, but I just can't seem to wrap my head around how it works, and more importantly, why the change breaks it. I also tried using the curly brackets as parentheses, but they don't appear to function that way. Here's the code:

    Code:
    <script>
    jQuery(document).ready( function($) {
    	jQuery(".product-field-type-E").css("display","none");
    	vm_age_value=$(".vmcustomlist option:selected").text();
    	if(vm_age_value=='0'){
    		virtuemart_cf_formula = $('#show_values_formula').val();
    		virtuemart_cf_price = $('#show_values_product_price').val();
    		show_url=$("#show_url").val();
    		virtuemart_cf_split=String(virtuemart_cf_formula).split("{");
    		virtuemart_cf_f_cal=String(virtuemart_cf_split[1]).split("}");
    		calculation_operator=virtuemart_cf_f_cal[1];
    		virtuemart_cf_s_cal=String(virtuemart_cf_split[2]).split("}");
    		calculation_operator_s=virtuemart_cf_s_cal[1];
    		calculation_one_year=virtuemart_cf_s_cal[0];
    		var customval=$(".other-customfield").html();
    		var customvalue=String(customval).split("$");
    		var output='';
    					/* ---Calculation as per the formula admin- */
    			if(calculation_operator	==	'*' && calculation_operator_s	==	'+'){
    				//output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) + parseInt(virtuemart_cf_price);
    				output = parseInt(vm_age_value) * parseInt(calculation_one_year) + parseInt(show_url);
    			}
    			if(calculation_operator	==	'*' && calculation_operator_s	==	'-'){
    				output = parseInt(vm_age_value) * parseInt(calculation_one_year) - parseInt(show_url);
    			}
    			if(calculation_operator	==	'*' && calculation_operator_s	==	'*'){
    				output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) * parseInt(show_url);
    			}
    			if(calculation_operator	==	'*' && calculation_operator_s	==	'/'){
    				output = parseInt(vm_age_value) * parseInt(calculation_one_year) / parseInt(show_url);
    			}
    			/* ---- */
    			if(calculation_operator	==	'+' && calculation_operator_s	==	'+'){
    				output = parseInt(vm_age_value) + parseInt(calculation_one_year) + parseInt(show_url);
    			}
    			if(calculation_operator	==	'+' && calculation_operator_s	==	'-'){
    				output = parseInt(vm_age_value) + parseInt(calculation_one_year) - parseInt(show_url);
    			}
    			if(calculation_operator	==	'+' && calculation_operator_s	==	'*'){
    				output = parseInt(vm_age_value) + parseInt(calculation_one_year) * parseInt(show_url);
    			}
    			if(calculation_operator	==	'+' && calculation_operator_s	==	'/'){
    				output = parseInt(vm_age_value) + parseInt(calculation_one_year) / parseInt(show_url);
    			}
    			/* ---- */
    			if(calculation_operator	==	'-' && calculation_operator_s	==	'+'){
    				output = parseInt(vm_age_value) - parseInt(calculation_one_year) + parseInt(show_url);
    			}
    			if(calculation_operator	==	'-' && calculation_operator_s	==	'-'){
    				output = parseInt(vm_age_value) - parseInt(calculation_one_year) - parseInt(show_url);
    			}
    			if(calculation_operator	==	'-' && calculation_operator_s	==	'*'){
    				output = parseInt(vm_age_value) - parseInt(calculation_one_year) * parseInt(show_url);
    			}
    			if(calculation_operator	==	'-' && calculation_operator_s	==	'/'){
    				output = parseInt(vm_age_value) - parseInt(calculation_one_year) / parseInt(show_url);
    			}
    			/* ---- */
    			if(calculation_operator	==	'/' && calculation_operator_s	==	'+'){
    				output = parseInt(vm_age_value) / parseInt(calculation_one_year) + parseInt(show_url);
    			}
    			if(calculation_operator	==	'/' && calculation_operator_s	==	'-'){
    				output = parseInt(vm_age_value) / parseInt(calculation_one_year) - parseInt(show_url);
    			}
    			if(calculation_operator	==	'/' && calculation_operator_s	==	'*'){
    				output = parseInt(vm_age_value) / parseInt(calculation_one_year) * parseInt(show_url);
    			}
    			if(calculation_operator	==	'/' && calculation_operator_s	==	'/'){
    				output = parseInt(vm_age_value) / parseInt(calculation_one_year) / parseInt(show_url);
    			}
    		    net_output="$"+output;
    		 //$('.vmcustom-textareainput').val(output);
    		 $('.vmcustom-textareainput').val(net_output);
    			
    		}
    	//z=jQuery(".vmcustomlist option:selected").text();
    	//alert(z);
    	//alert("hi");
    	//jQuery(".product-fields-title:last").css('display','none');
    	//jQuery(".hasTip:last").css('display','none');
    	//x=jQuery(".vmcustom-textinput").val();
    	//if(x==""){
    	//y="zero";
    	//alert(y);
    	//jQuery(".vmcustom-textareainput").val(y);
    	//}
    	jQuery('.vmcustom-textareainput').css('display','none');
    	//jQuery('.vmcustom-textinput').keyup(function() {
    	jQuery('.vmcustomlist').change(function() {
    			formProduct = $(this).parents('form.product');
    			virtuemart_cf_id = $('#show_values_cfId').val();
    			nameOfCF = "customPlugin["+virtuemart_cf_id+"][textinput][comment]";
    			inputCF='\'input[name="'+nameOfCF+'"]\'';
    			//virtuemart_age_value = $(".vmcustom-textinput").val();
    			virtuemart_age_value = $(".vmcustomlist option:selected").text();
    			//alert(virtuemart_age_value);
    			virtuemart_cf_formula = $('#show_values_formula').val();
    			virtuemart_cf_price = $('#show_values_product_price').val();
    			show_url=$("#show_url").val();
    			virtuemart_cf_split=String(virtuemart_cf_formula).split("{");
    			virtuemart_cf_f_cal=String(virtuemart_cf_split[1]).split("}");
    			calculation_operator=virtuemart_cf_f_cal[1];
    			virtuemart_cf_s_cal=String(virtuemart_cf_split[2]).split("}");
    			calculation_operator_s=virtuemart_cf_s_cal[1];
    			calculation_one_year=virtuemart_cf_s_cal[0];
    			var customval=$(".other-customfield").html();
    			var customvalue=String(customval).split("$");
    			var output='';
    				
    			/* ---Calculation as per the formula admin- */
    			if(calculation_operator	==	'*' && calculation_operator_s	==	'+'){
    				//output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) + parseInt(virtuemart_cf_price);
    				output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) + parseInt(show_url);
    			}
    			if(calculation_operator	==	'*' && calculation_operator_s	==	'-'){
    				output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) - parseInt(show_url);
    			}
    			if(calculation_operator	==	'*' && calculation_operator_s	==	'*'){
    				output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) * parseInt(show_url);
    			}
    			if(calculation_operator	==	'*' && calculation_operator_s	==	'/'){
    				output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) / parseInt(show_url);
    			}
    			/* ---- */
    			if(calculation_operator	==	'+' && calculation_operator_s	==	'+'){
    				output = parseInt(virtuemart_age_value) + parseInt(calculation_one_year) + parseInt(show_url);
    			}
    			if(calculation_operator	==	'+' && calculation_operator_s	==	'-'){
    				output = parseInt(virtuemart_age_value) + parseInt(calculation_one_year) - parseInt(show_url);
    			}
    			if(calculation_operator	==	'+' && calculation_operator_s	==	'*'){
    				output = parseInt(virtuemart_age_value) + parseInt(calculation_one_year) * parseInt(show_url);
    			}
    			if(calculation_operator	==	'+' && calculation_operator_s	==	'/'){
    				output = parseInt(virtuemart_age_value) + parseInt(calculation_one_year) / parseInt(show_url);
    			}
    			/* ---- */
    			if(calculation_operator	==	'-' && calculation_operator_s	==	'+'){
    				output = parseInt(virtuemart_age_value) - parseInt(calculation_one_year) + parseInt(show_url);
    			}
    			if(calculation_operator	==	'-' && calculation_operator_s	==	'-'){
    				output = parseInt(virtuemart_age_value) - parseInt(calculation_one_year) - parseInt(show_url);
    			}
    			if(calculation_operator	==	'-' && calculation_operator_s	==	'*'){
    				output = parseInt(virtuemart_age_value) - parseInt(calculation_one_year) * parseInt(show_url);
    			}
    			if(calculation_operator	==	'-' && calculation_operator_s	==	'/'){
    				output = parseInt(virtuemart_age_value) - parseInt(calculation_one_year) / parseInt(show_url);
    			}
    			/* ---- */
    			if(calculation_operator	==	'/' && calculation_operator_s	==	'+'){
    				output = parseInt(virtuemart_age_value) / parseInt(calculation_one_year) + parseInt(show_url);
    			}
    			if(calculation_operator	==	'/' && calculation_operator_s	==	'-'){
    				output = parseInt(virtuemart_age_value) / parseInt(calculation_one_year) - parseInt(show_url);
    			}
    			if(calculation_operator	==	'/' && calculation_operator_s	==	'*'){
    				output = parseInt(virtuemart_age_value) / parseInt(calculation_one_year) * parseInt(show_url);
    			}
    			if(calculation_operator	==	'/' && calculation_operator_s	==	'/'){
    				output = parseInt(virtuemart_age_value) / parseInt(calculation_one_year) / parseInt(show_url);
    			}
    			if(virtuemart_age_value!=""){
    			//var result	=	'Current value is = ' + output;
    			 net_output="$"+output;
    		 	//$('.vmcustom-textareainput').val(output);
    		 	$('.vmcustom-textareainput').val(net_output);
    			
    			//$('#showValueDisplay').html(result);
    			//document.getElementById("showValueDisplay").innerHTML=result;
    			}
    			else{
    				//var result= "";
    				//document.getElementById("showValueDisplay").innerHTML=result;
    			}
    			
    		});
    
    });
    </script>
    The only other piece that appears to be related to it at the bottom of the page is:

    Code:
    <input type="hidden" name="show_values_formula" id="show_values_formula" value="<?php echo $this->product->intnotes; ?>" />
    <input type="hidden" name="show_values_cfId" id="show_values_cfId" value="<?php echo $this->product->virtuemart_customfield_id; ?>" />
    <input type="hidden" name="show_values_product_price" id="show_values_product_price" value="<?php echo $this->product->product_price; ?>" />
    <input type="hidden" name="show_url" id="show_url" value="<?php echo $this->product->product_url; ?>" />
    <?php //echo $this->product->virtuemart_customfield_id; ?>
    <span id="showValueDisplay"></span>
    ...but I'm pretty sure that just made stuff visible/invisible for testing purposes.

    As for why I don't go back to the contractor who did it, the language barrier was just too much to deal with, so I thought I'd try to fix it myself.

    Again, I'm not asking for someone to fix it, just a little help in understanding what I'm seeing so I can take a stab at it myself.

    Thanks.
    Last edited by Obi42; 02-19-2013 at 11:56 PM.

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Welcome to the forum!

    You're looking for javascript help, not PHP help. Ask a moderator to move your thread to the javascript section (don't cross-post).

    Here is your cleaned-up javascript (there was a LOT of commented-out junk left in there and doing this cleanup will make things easier for someone to help you with the actual code at work - remeber this in future):
    Code:
    <script>
    jQuery(document).ready( function($){
    	jQuery(".product-field-type-E").css("display","none");
    	vm_age_value=$(".vmcustomlist option:selected").text();
    	if(vm_age_value=='0'){
    		virtuemart_cf_formula = $('#show_values_formula').val();
    		virtuemart_cf_price = $('#show_values_product_price').val();
    		show_url=$("#show_url").val();
    		virtuemart_cf_split=String(virtuemart_cf_formula).split("{");
    		virtuemart_cf_f_cal=String(virtuemart_cf_split[1]).split("}");
    		calculation_operator=virtuemart_cf_f_cal[1];
    		virtuemart_cf_s_cal=String(virtuemart_cf_split[2]).split("}");
    		calculation_operator_s=virtuemart_cf_s_cal[1];
    		calculation_one_year=virtuemart_cf_s_cal[0];
    		var customval=$(".other-customfield").html();
    		var customvalue=String(customval).split("$");
    		var output='';
    					/* ---Calculation as per the formula admin- */
    		if(calculation_operator	==	'*' && calculation_operator_s	==	'+'){
    			output = parseInt(vm_age_value) * parseInt(calculation_one_year) + parseInt(show_url);
    		}
    		if(calculation_operator	==	'*' && calculation_operator_s	==	'-'){
    			output = parseInt(vm_age_value) * parseInt(calculation_one_year) - parseInt(show_url);
    		}
    		if(calculation_operator	==	'*' && calculation_operator_s	==	'*'){
    			output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) * parseInt(show_url);
    		}
    		if(calculation_operator	==	'*' && calculation_operator_s	==	'/'){
    			output = parseInt(vm_age_value) * parseInt(calculation_one_year) / parseInt(show_url);
    		}
    		/* ---- */
    		if(calculation_operator	==	'+' && calculation_operator_s	==	'+'){
    			output = parseInt(vm_age_value) + parseInt(calculation_one_year) + parseInt(show_url);
    		}
    		if(calculation_operator	==	'+' && calculation_operator_s	==	'-'){
    			output = parseInt(vm_age_value) + parseInt(calculation_one_year) - parseInt(show_url);
    		}
    		if(calculation_operator	==	'+' && calculation_operator_s	==	'*'){
    			output = parseInt(vm_age_value) + parseInt(calculation_one_year) * parseInt(show_url);
    		}
    		if(calculation_operator	==	'+' && calculation_operator_s	==	'/'){
    			output = parseInt(vm_age_value) + parseInt(calculation_one_year) / parseInt(show_url);
    		}
    		/* ---- */
    		if(calculation_operator	==	'-' && calculation_operator_s	==	'+'){
    			output = parseInt(vm_age_value) - parseInt(calculation_one_year) + parseInt(show_url);
    		}
    		if(calculation_operator	==	'-' && calculation_operator_s	==	'-'){
    			output = parseInt(vm_age_value) - parseInt(calculation_one_year) - parseInt(show_url);
    		}
    		if(calculation_operator	==	'-' && calculation_operator_s	==	'*'){
    			output = parseInt(vm_age_value) - parseInt(calculation_one_year) * parseInt(show_url);
    		}
    		if(calculation_operator	==	'-' && calculation_operator_s	==	'/'){
    			output = parseInt(vm_age_value) - parseInt(calculation_one_year) / parseInt(show_url);
    		}
    		/* ---- */
    		if(calculation_operator	==	'/' && calculation_operator_s	==	'+'){
    			output = parseInt(vm_age_value) / parseInt(calculation_one_year) + parseInt(show_url);
    		}
    		if(calculation_operator	==	'/' && calculation_operator_s	==	'-'){
    			output = parseInt(vm_age_value) / parseInt(calculation_one_year) - parseInt(show_url);
    		}
    		if(calculation_operator	==	'/' && calculation_operator_s	==	'*'){
    			output = parseInt(vm_age_value) / parseInt(calculation_one_year) * parseInt(show_url);
    		}
    		if(calculation_operator	==	'/' && calculation_operator_s	==	'/'){
    			output = parseInt(vm_age_value) / parseInt(calculation_one_year) / parseInt(show_url);
    		}
    		net_output="$"+output;
    		$('.vmcustom-textareainput').val(net_output);
    	}
    	jQuery('.vmcustom-textareainput').css('display','none');
    	jQuery('.vmcustomlist').change(function(){
    		formProduct = $(this).parents('form.product');
    		virtuemart_cf_id = $('#show_values_cfId').val();
    		nameOfCF = "customPlugin["+virtuemart_cf_id+"][textinput][comment]";
    		inputCF='\'input[name="'+nameOfCF+'"]\'';
    		virtuemart_age_value = $(".vmcustomlist option:selected").text();
    		virtuemart_cf_formula = $('#show_values_formula').val();
    		virtuemart_cf_price = $('#show_values_product_price').val();
    		show_url=$("#show_url").val();
    		virtuemart_cf_split=String(virtuemart_cf_formula).split("{");
    		virtuemart_cf_f_cal=String(virtuemart_cf_split[1]).split("}");
    		calculation_operator=virtuemart_cf_f_cal[1];
    		virtuemart_cf_s_cal=String(virtuemart_cf_split[2]).split("}");
    		calculation_operator_s=virtuemart_cf_s_cal[1];
    		calculation_one_year=virtuemart_cf_s_cal[0];
    		var customval=$(".other-customfield").html();
    		var customvalue=String(customval).split("$");
    		var output='';
    
    		/* ---Calculation as per the formula admin- */
    		if(calculation_operator	==	'*' && calculation_operator_s	==	'+'){
    			output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) + parseInt(show_url);
    		}
    		if(calculation_operator	==	'*' && calculation_operator_s	==	'-'){
    			output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) - parseInt(show_url);
    		}
    		if(calculation_operator	==	'*' && calculation_operator_s	==	'*'){
    			output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) * parseInt(show_url);
    		}
    		if(calculation_operator	==	'*' && calculation_operator_s	==	'/'){
    			output = parseInt(virtuemart_age_value) * parseInt(calculation_one_year) / parseInt(show_url);
    		}
    		/* ---- */
    		if(calculation_operator	==	'+' && calculation_operator_s	==	'+'){
    			output = parseInt(virtuemart_age_value) + parseInt(calculation_one_year) + parseInt(show_url);
    		}
    		if(calculation_operator	==	'+' && calculation_operator_s	==	'-'){
    			output = parseInt(virtuemart_age_value) + parseInt(calculation_one_year) - parseInt(show_url);
    		}
    		if(calculation_operator	==	'+' && calculation_operator_s	==	'*'){
    			output = parseInt(virtuemart_age_value) + parseInt(calculation_one_year) * parseInt(show_url);
    		}
    		if(calculation_operator	==	'+' && calculation_operator_s	==	'/'){
    			output = parseInt(virtuemart_age_value) + parseInt(calculation_one_year) / parseInt(show_url);
    		}
    		/* ---- */
    		if(calculation_operator	==	'-' && calculation_operator_s	==	'+'){
    			output = parseInt(virtuemart_age_value) - parseInt(calculation_one_year) + parseInt(show_url);
    		}
    		if(calculation_operator	==	'-' && calculation_operator_s	==	'-'){
    			output = parseInt(virtuemart_age_value) - parseInt(calculation_one_year) - parseInt(show_url);
    		}
    		if(calculation_operator	==	'-' && calculation_operator_s	==	'*'){
    			output = parseInt(virtuemart_age_value) - parseInt(calculation_one_year) * parseInt(show_url);
    		}
    		if(calculation_operator	==	'-' && calculation_operator_s	==	'/'){
    			output = parseInt(virtuemart_age_value) - parseInt(calculation_one_year) / parseInt(show_url);
    		}
    		/* ---- */
    		if(calculation_operator	==	'/' && calculation_operator_s	==	'+'){
    			output = parseInt(virtuemart_age_value) / parseInt(calculation_one_year) + parseInt(show_url);
    		}
    		if(calculation_operator	==	'/' && calculation_operator_s	==	'-'){
    			output = parseInt(virtuemart_age_value) / parseInt(calculation_one_year) - parseInt(show_url);
    		}
    		if(calculation_operator	==	'/' && calculation_operator_s	==	'*'){
    			output = parseInt(virtuemart_age_value) / parseInt(calculation_one_year) * parseInt(show_url);
    		}
    		if(calculation_operator	==	'/' && calculation_operator_s	==	'/'){
    			output = parseInt(virtuemart_age_value) / parseInt(calculation_one_year) / parseInt(show_url);
    		}
    		if(virtuemart_age_value!=""){
    			net_output="$"+output;
    			$('.vmcustom-textareainput').val(net_output);
    		}
    	});
    });
    </script>
    Note that I am focusing onthe highlighted section of the code above, as that is the portion that "calculates" your formula when you change it in the page. I am not even going to bother looking into the calculation that runs on page load because it looks almost identical anyway and I don't think it is the focus of your question.

    That said, the script basically pulls in your formula to the variable "virtuemart_cf_formula" and then splits the string a number of different ways in order to isolate operators and values, and then it uses a couple of values obtained from a select element and an input element to do some math.

    As far as I can see, there is no code in there at all to actually pay attention to what names you have listed in your formula. In fact, the script only supports the use of numbers (not names or any non-numeric data) in the forumla anyway. Further, this script is not set up to use a flexible number of calculation pieces at all. It will only ever accept two operators and it will only ever pay attention to the value in the second set of curly braces (which, again, MUST be a number for this script to work at all - more on this later). If you paid any money to the coder who wrote this for you I'd recommend you try to get it back immediately. This script does not appear to do what you say it should do, and this is a good reason NOT to use the cheapest off-shore coder you can find to do some work (admittedly, an assumption based on the language barrier you mention - but we see a lot of this in the forum).

    So, here is the bottom line:
    This script only supports the use of two operators at a time (you can see this with all of the if(calculation_operator == '*' && calculation_operator_s == '+'){ tests in the list of "if" statements)...there is no array being passed for checking, and no recursion...only two operators are being tested...ever. And whatever your values are here: {Product_Age}+{1}*{Product_Value} is irrelevant because the script never does anything with them in this formula. It never even isolates them as values, which in this context would be done with virtuemart_cf_f_cal[0] and virtuemart_cf_split[3] , respectively (though hard-coding this index into the script means, again, that the script is not flexible in its number of inputs and operators). Even if you were to hard-code those items the script would still need to be updated to actually use them, so that's still not a very quick fix. The script only picks out the first two operators, and then produces a calculation basically consisting of this process:

    "show_url" value <first operator> value in the SECOND curly braces <second operator> "virtuemart_age_value" value

    Edit: To illustrate this fact, you can add this line right before the list of "if" statements in the change function:
    Code:
    alert(parseInt(virtuemart_age_value)+" "+calculation_operator+" "+parseInt(calculation_one_year)+" "+calculation_operator_s+" "+parseInt(show_url));
    That will alert the formula being used to generate the final result, and unless I have made a mistake, if you hand-calculate what appears in the alert box it should come out to the same result you are given in the page. This will also show where the NaN error comes into play if your formula has a non-numeric value for the second curly braces.


    So in the case of your first example formula the script just multiplies the value of "show_url" by whatever numeric value is in the SECOND curly braces, and then adds the value of "virtuemart_age_value" to the result (since the first operator was a multiplication operation and the second one was addition). If you reverse the order of the operators it adds "show_url" to the product of the value in the second curly braces and the value of "virtuemart_age_value" (in other words, javascript automatically follows the standard "order of operations" and this script merely moves around the */+- operators to fit whichever order they appeared in the formula you enter).

    If this script ever produced a correct result for you then it was probably a coincidence.

    In response to your specific issue, since the second thing in curly braces is a string when you use the forumla {10}-{Product_Age}/{10}*{Product_Value} you are attempting to perform mathematics on a string and are receving a NaN error as your output. It only continued to "work" when numbers were added to the middle of the formula because those items were still numeric and/or the "1" was still in the second set of curly braces - you can literally add anything you want in curly braces after that second set of curly braces and the script will still run.

    This script cannot be "tweaked" into working the way you want it to work, and it really needs a complete restart. Sorry for the bad news!
    Last edited by Rowsdower!; 02-20-2013 at 02:45 PM. Reason: added a code snippet for illustration
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • The Following 2 Users Say Thank You to Rowsdower! For This Useful Post:

    Fumigator (02-20-2013), Obi42 (02-20-2013)

  • #3
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Wow, thank you for your analysis (and thank you to whomever moved the thread, sorry about that)...

    I was afraid it wouldn't be an easy fix, but you've given me a good starting point, and more importantly, I think I'm finally starting to understand javascript (no small feat considering the last coding I did was TrueBasic and a little pascal a decade ago).

    Your assumption was correct, though the work he did for us up to this point was good, but if I have to farm this out I'll be staying in country, or at least language.

    Now I need to reread this a couple more times and dive in.... again, thank you!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    There *is* an easy way to do this.

    It's kind of a hack, and not recommended, but truth to tell...it works.

    And the answer is to use JavaScript's eval( ) function.

    Let's say you have the formula:
    Code:
    {10}-{Product_Age}/{10}*{Product_Value}
    First of all, change the formula so that constants (numbers) do *NOT* have braces around them:
    Code:
    10-{Product_Age}/10*{Product_Value}
    You could put parentheses any place in the formula, if needed for priority of operators and/or clarity. Example:
    Code:
    ( 10-{Product_Age}/10 ) * {Product_Value}
    And, of course, assign all that to some string:
    Code:
    var formula = "10-{Product_Age}/10*{Product_Value}";
    Then we will assume that any name remaining in braces is the name of a <form> field on the page:
    Code:
    <input name="Product_Age"/>
    <input name="Product_Value"/>
    So now all we have to do is substitute in the numeric value of those form fields for the names in braces:
    Code:
    formula = formula.replace(/\{([^\}]+)\}/g, "Number(forms[0].$1.value)");
    If you care, that would convert
    Code:
    10-{Product_Age}/10*{Product_Value}
    to
    Code:
    10-Number(forms[0].Product_Age.value)/10*Number(forms[0].Product_Value.value)
    And then all you have to do to get the value of that formula:
    Code:
    var formulaValue = eval( formula );
    It's a pretty minor hack, considering what you will get from it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Obi42 (02-21-2013)


  •  

    Posting Permissions

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