Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating text formula

Status
Not open for further replies.

manishblr1

Programmer
Dec 20, 2010
20
0
0
US
Hi ,

I have been stuck up with the following issues.

Scenario:

I have a formula box where user enters the formula for example :
(plant1*2+plant2*3) and then user clicks on validate button.

I am replacing plant1/plant2 variables by 1 and trying to get the output as 5. But its giving NaN. I know that the above formula is a string . But i want to know is there any way we can achieve the output as number when forumla is given by the user.

Code:
<HTML>
<Title>Consumption Booking</Title>
<script language="javascript">
//definition of global variables 
var plantList = new Array();
plantList[0]='';

function plantSelectedFromList(){
	if(document.getElementById('myPlantListDropdown').value =="Select Plant"){
		//do nothing
	}
	else{
		var selectedPlant = document.getElementById('myPlantListDropdown').value;
		if(plantList[0].length==0)
			plantList[0]= plantList[0]+selectedPlant;
		else
			plantList[0]= plantList[0]+","+selectedPlant;
		var formulaTextarea = consumptionBookingForm.myFormulaTextarea.value;
		formulaTextarea = formulaTextarea + selectedPlant+" ";
		consumptionBookingForm.myFormulaTextarea.value = formulaTextarea;
	}
}


//function to validate quantity calculator formula
function validateQuantityFormula(){
	alert(consumptionBookingForm.myFormulaTextarea.value);
	var tempQuantityCalc= consumptionBookingForm.myFormulaTextarea.value;
	var listOfPlants=plantList[0].split(",");
	for(i=0;i<listOfPlants.length;i++){
		tempQuantityCalc=tempQuantityCalc.replace(listOfPlants[i],1);
	}
	alert(tempQuantityCalc);
//	var numberOccurence= tempQuantityCalc.match(/[\d\.]+/g);
}

</script>
<BODY>
<form name="consumptionBookingForm">
<TABLE cellpadding="0" cellspacing="0" width="50%" height="50%" border="1" bordercolor="#3399FF">
	<TR id="cosumptionBookingTag">
		<TD>
			<select id="myPlantListDropdown" onchange="plantSelectedFromList()">
				<option value="Select Plant">Select Plant</option>
				<option value="CCRC">CCRC</option>
				<option value="CCRP">CCRP</option>
				<option value="PHPL">PHPL</option>
				<option value="RFMY">RFMY</option>
				<option value="SAPL">SAPL</option>
				<option value="SMLT">SMLT</option>
				<option value="TEOU">TEOU</option>
			</select>
		</TD>
	</TR>
	<TR class="ContentHeaderSection">
		<TD align="center"><label>Quantity Calculator Formula</label><font color="red">*</font> </TD>
	</TR>
	<TR>
		<TD><textarea cols="90" rows="6" id="myFormulaTextarea"></textarea> </TD>
	</TR>
	<TR>
		<TD align="center"><span class="myButton"><input type="button" id="myValidateCalcButton" onclick="validateQuantityFormula()" value="Validate"></span> </TD>
	</TR>
</TABLE>
 </form>
</BODY>
</HTML>

Thanks in advance.
 
Hi

manishblr1 said:
(plant1*2+plant2*3)
I suppose that should be (CCRC*2+CCRP*3).

I think the base idea of using plantList is flowed. ( Or I just misunderstood the functionality. )

You could either use an object with the Plant values as properties and do the replacement from that :
Code:
[b]var[/b] plantList[teal]=[/teal][teal]{[/teal]
  CCRC[teal]:[/teal][purple]1[/purple][teal],[/teal]
  CCRP[teal]:[/teal][purple]2[/purple][teal],[/teal]
  PHPL[teal]:[/teal][purple]9[/purple][teal],[/teal]
  RFMY[teal]:[/teal][purple]63[/purple][teal],[/teal]
  SAPL[teal]:[/teal][purple]7[/purple][teal],[/teal]
  SMLT[teal]:[/teal][purple]2011[/purple][teal],[/teal]
  TEOU[teal]:[/teal][purple]3[/purple]
[teal]}[/teal]

[b]function[/b] [COLOR=darkgoldenrod]validateQuantityFormula[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] tempQuantityCalc[teal]=[/teal]consumptionBookingForm[teal].[/teal]myFormulaTextarea[teal].[/teal]value[teal];[/teal]
  tempQuantityCalc[teal]=[/teal]tempQuantityCalc[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/[A-Z]{4}/g[/fuchsia][teal],[/teal][b]function[/b][teal]([/teal]p[teal])[/teal][teal]{[/teal][b]if[/b][teal]([/teal]p [b]in[/b] plantList[teal])[/teal][b]return[/b] plantList[teal][[/teal]p[teal]];[/teal][b]return[/b] p[teal]}[/teal][teal]);[/teal]
  [COLOR=darkgoldenrod]alert[/color][teal]([/teal]tempQuantityCalc[teal]+[/teal][green][i]' = '[/i][/green][teal]+[/teal][COLOR=darkgoldenrod]eval[/color][teal]([/teal]tempQuantityCalc[teal]));[/teal]
[teal]}[/teal]
Or declare variables for each Plant and let [tt]eval()[/tt] do its job :
Code:
[b]var[/b]
  CCRC[teal]=[/teal][purple]1[/purple][teal],[/teal]
  CCRP[teal]=[/teal][purple]2[/purple][teal],[/teal]
  PHPL[teal]=[/teal][purple]9[/purple][teal],[/teal]
  RFMY[teal]=[/teal][purple]63[/purple][teal],[/teal]
  SAPL[teal]=[/teal][purple]7[/purple][teal],[/teal]
  SMLT[teal]=[/teal][purple]2011[/purple][teal],[/teal]
  TEOU[teal]=[/teal][purple]3[/purple]

[b]function[/b] [COLOR=darkgoldenrod]validateQuantityFormula[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] tempQuantityCalc[teal]=[/teal]consumptionBookingForm[teal].[/teal]myFormulaTextarea[teal].[/teal]value[teal];[/teal]
  [COLOR=darkgoldenrod]alert[/color][teal]([/teal]tempQuantityCalc[teal]+[/teal][green][i]' = '[/i][/green][teal]+[/teal][COLOR=darkgoldenrod]eval[/color][teal]([/teal]tempQuantityCalc[teal]));[/teal]
[teal]}[/teal]
As a personal opinion, I prefer to use better delimited placeholders, like ([highlight]{{[/highlight]CCRC[highlight]}}[/highlight]*2+[highlight]{{[/highlight]CCRP[highlight]}}[/highlight]*3) or at least ([highlight]$[/highlight]CCRC*2+[highlight]$[/highlight]CCRP*3).


Feherke.
 
Thanks a ton. It worked.

sorry for not explaining the scenario properly.But the way you understood is right and that is what i was trying to explain.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top