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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculator with comparison response

Status
Not open for further replies.

mtdew

Technical User
Dec 9, 2007
77
0
0
US
I'm trying to create a simple calculation. I have the calculation part working correctly but I want to compare the result and have a message appear based on the result. I've tried several examples such as:

if (P.toFixed(0)) == 1
$("#coolingneed").val('1 ton unit');
else
if (P.toFixed(0)) >= 2
$("#coolingneed").val('1.5 ton unit');

but nothing works. Actually the calculation will not perform when I try to add the comparison statements.

Suggestions?




<script type='text/javascript' src='

<h3>Cooling Calculator</h3>
<form >
<p> <input type="text" name="people" id="people" class="tonfield" />
Number of people you are cooling
</p>
<p> <input type="text" name="watts" id="watts" class="tonfield" />
Total wattage for all equipment in room
</p>
<p> <input type="text" name="sqft" id="sqft" class="tonfield" />
Square footage of room
</p>

<button class="smallButton" id="coolingcalc" onclick="return false">Tons of Cooling</button>
<input type="text" name="coolingneed" id="coolingneed" class="tonAnswer" />
</form>
<script type="text/javascript">
$("#coolingcalc").click(function(){
var L,P,n,c,dp;
L = parseInt($("#people").val()) * 600;
n = parseInt($("#watts").val()) * .34;
c = parseFloat($("#sqft").val())/250;

P = (L+n+c)/12000;



if(!isNaN(P))
{
$("#coolingneed").val(P.toFixed(0));
}
else
{
$("#coolingneed").val('There was an error');


}
return false;
});
</script>
 
Always check the errors returned by the browser. At the very least you should be getting a syntax error with those If statements:

Code:
if [red]([/red]P.toFixed(0)[red])[/red] == 1  
$("#coolingneed").val('1 ton unit');
else if[red]([/red]P.toFixed(0)[red])[/red] >= 2
$("#coolingneed").val('1.5 ton unit');
Basically you are saying that if p.toFixed(0) returns any value execute the "==1" line which is of course invalid code.

They should be:
Code:
if [red]([/red]P.toFixed(0)== 1[red])[/red]

Also its always a good idea to surround the statements that are to be executed in if block with curly braces {}

Code:
if(P.toFixed(0) == 1 ){
$("#coolingneed").val('1 ton unit');
}
else if(P.toFixed(0) >= 2){
$("#coolingneed").val('1.5 ton unit');
}



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top