jQuery - I'm getting unexpected outputs from a basic math formula.
        Posted  
        
            by OllieMcCarthy
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by OllieMcCarthy
        
        
        
        Published on 2010-05-21T13:31:26Z
        Indexed on 
            2010/05/21
            13:50 UTC
        
        
        Read the original article
        Hit count: 331
        
JavaScript
|math
Hi I would like to start by saying I'd greatly appreciate anyones help on this. I have built a small caculator to calculate the amount a consumer can save annually on energy by installing a ground heat pump or solar panels.
As far as I can tell the mathematical formulas are correct and my client verified this yesterday when I showed him the code. Two problems. The first is that the calculator is outputting ridiculously large numbers for the first result. The second problem is that the solar result is only working when there are no zeros in the fields.
Are there some quirks as to how one would write mathematical formulas in JS or jQuery?
Any help greatly appreciated. Here is the link - http://www.olliemccarthy.com/test/johncmurphy/?page_id=249
And here is the code for the entire function -
$jq(document).ready(function(){ 
// Energy Bill Saver 
// Declare Variables 
var A = "";  // Input for Oil
var B = "";  // Input for Storage Heater
var C = "";  // Input for Natural Gas
var D = "";  // Input for LPG
var E = "";  // Input for Coal 
var F = "";  // Input for Wood Pellets
var G = "";  // Input for Number of Occupants 
var J = "";  
var K = "";
var H = "";
var I = "";
// Declare Constants 
var a = "0.0816"; // Rate for Oil
var b = "0.0963"; // Rate for NightRate
var c = "0.0558"; // Rate for Gas
var d = "0.1579";  // Rate for LPG
var e = "0.121";  // Rate for Coal
var f = "0.0828";  // Rate for Pellets
var g = "0.02675";  // Rate for Heat Pump
var x = "1226.4"; 
// Splittin up I to avoid error 
var S1 = ""; // Splitting up the calcuation for I
var S2 = ""; // Splitting up the calcuation for I
var S3 = ""; // Splitting up the calcuation for I
var S4 = ""; // Splitting up the calcuation for I
var S5 = ""; // Splitting up the calcuation for I
var S6 = ""; // Splitting up the calcuation for I
// Calculate H (Ground Sourced Heat Pump)
$jq(".es-calculate").click(function(){
    $jq(".es-result-wrap").slideDown(300);
    A = $jq("input.es-oil").val();
    B = $jq("input.es-storage").val();
    C = $jq("input.es-gas").val();
    D = $jq("input.es-lpg").val();
    E = $jq("input.es-coal").val();
    F = $jq("input.es-pellets").val();
    G = $jq("input.es-occupants").val();
    J = ( A / a ) + ( B / b ) + ( C / c ) + ( D / d ) + ( E / e ) + ( F / f ) ;
    H = A + B + C + D + E + F - ( J * g ) ;
    K = ( G * x ) ;
    if ( A !== "0" ) { S1 = ( ( ( A / a ) / J ) * K * a ) ; }
    else { S1 = "0" ; }
    if ( B !== "0" ) { S2 = ( ( ( B / b ) / J ) * K * b ) ; }
    else { S2 = "0" ; }
    if ( C !== "0" ) { S3 = ( ( ( C / c ) / J ) * K * c ) ; }
    else { S3 = "0" ; }
    if ( D !== "0" ) { S4 = ( ( ( D / d ) / J ) * K * d ) ; }
    else { S4 = "0" ; }
    if ( E !== "0" ) { S5 = ( ( ( E / e ) / J ) * K * e ) ; }
    else { S5 = "0" ; }
    if ( F !== "0" ) { S6 = ( ( ( F / f ) / J ) * K * f ) ; }
    else { S6 = "0" ; }
    I = S1 + S2 + S3 + S4 + S5 + S6 ;
    if(!isNaN(H)) {$jq("span.es-result-span-h").text(H.toFixed(2));}
    else{$jq("span.es-result-span-h").text('Error: Please enter numerals only');}
    if(!isNaN(I)) {$jq("span.es-result-span-i").text(I.toFixed(2));}
    else{$jq("span.es-result-span-i").text('Error: Please enter numerals only');}
    });
});
© Stack Overflow or respective owner