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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    9
    Thanked 0 Times in 0 Posts

    How do you add values from separate functions?

    Can someone help. Why is this code not adding/updating the two values from these functions?


    <script type="text/javascript">
    var LHtotal;
    var AHtotal;
    var LMtotal;
    var AMtotal;
    var schoolTotal;
    var churchTotal;
    var grandTotal;
    var a;
    a = new Boolean();
    a = false;
    var newTotal1;
    var newTotal2;
    function calculateSchool(orgType) {
    var fund = parseInt(document.getElementById(orgType + 'fund').value);
    var people = parseInt(document.getElementById(orgType + 'people').value);
    var percent = (parseInt(document.getElementById(orgType + 'percent').value))/100;
    var active = people * percent;
    active = parseInt(active);
    var baskets = parseInt(document.getElementById(orgType + 'baskets').value);
    var numPerYear = parseInt(document.getElementById(orgType + 'numPerYear').value);
    var price = 26.00;
    var commision = 0.10;
    var total = fund * active * baskets * numPerYear * price * commision;
    total = total.toFixed(2);
    LHtotal = parseFloat(document.getElementById("LHtotal").innerHTML);
    AHtotal = parseFloat(document.getElementById("AHtotal").innerHTML);
    LMtotal = parseFloat(document.getElementById("LMtotal").innerHTML);
    AMtotal = parseFloat(document.getElementById("AMtotal").innerHTML);
    //alert(LHtotal + " " + AHtotal + " " + LMtotal + " " + AMtotal);
    schoolTotal = (LHtotal + AHtotal + LMtotal + AMtotal);
    //alert(schoolTotal);

    if(isNaN(schoolTotal)) {
    schoolTotal = "";
    }

    if(isNaN(active)) {
    active = "";
    }
    if(isNaN(total)) {
    total = "";
    }
    var Ototal = parseFloat(document.getElementById("Ototal").innerHTML);
    var Etotal = parseFloat(document.getElementById("Etotal").innerHTML);
    var orgTotal = (Ototal + Etotal);
    var orgTotal = orgTotal.toFixed(2);
    var grandTotal = (schoolTotal + orgTotal);
    if(isNaN(grandTotal)) {
    grandTotal = "";
    }
    if(isNaN(orgTotal)) {
    orgTotal = "";
    }
    document.getElementById("orgTotal").innerHTML = orgTotal;
    //document.getElementById("grandTotal").innerHTML = grandTotal;
    document.getElementById(orgType + 'total').innerHTML = total;
    document.getElementById(orgType + 'active').innerHTML = active;
    document.getElementById("schoolTotal").innerHTML = schoolTotal;
    superTotal(schoolTotal);
    a = true;
    ;


    }
    function calculateChurch(orgType) {
    var num = parseFloat(document.getElementById(orgType + "num").value);
    var members = parseFloat(document.getElementById(orgType + "members").value);
    var percent = parseFloat(document.getElementById(orgType + "percent").value)/100;
    var baskets = parseFloat(document.getElementById(orgType + "baskets").value);
    var fundPerYear = parseFloat(document.getElementById(orgType + "fundPerYear").value);
    var total = num * members * percent * baskets * fundPerYear;
    total = total.toFixed(2);
    if(isNaN(total)) {
    total = "";
    }
    document.getElementById(orgType + 'total').innerHTML = total;
    var Ltotal = parseFloat(document.getElementById('Ltotal').innerHTML);
    var Atotal = parseFloat(document.getElementById('Atotal').innerHTML);
    churchTotal = (Ltotal + Atotal);
    churchTotal = churchTotal.toFixed(2);

    if(isNaN(churchTotal)) {
    churchTotal = "";
    }
    document.getElementById("churchTotal").innerHTML = churchTotal;
    a = false;
    superTotal(churchTotal);
    }
    function superTotal(x){

    var sum = document.getElementById("grandTotal").innerHTML ;
    var get = x;
    var what = sum + get;
    document.getElementById("grandTotal").innerHTML = what;
    newTotal1 = what;
    }
    function superTotal2(y){
    newTotal2 = document.getElementById("grandTotal").innerHTML + y
    document.getElementById("grandTotal").value = whole;
    }
    var whole = newTotal1 + newTotal2;
    document.getElementById("grandTotal").innerHTML = whole;
    }

    </script>
    Last edited by greenhat; 09-11-2012 at 09:31 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Variables declared with the var keyword within a function are local to that function, and not available to other functions within the script.

    If you want a variable (e.g total) to be available to more than one function (that is global scope) you should declare it outside the functions. You have done this with several variables, e.g. LHtotal.

    total = total.toFixed(2); // total is now a string value
    if(isNaN(total)) { // so will always be NaN
    use .toFixed() only to format numbers for display.

    Similarly
    var orgTotal = orgTotal.toFixed(2); // now a string
    var grandTotal = (schoolTotal + orgTotal);
    will concatenate the two string variables, not add them.

    If you want to round a number to two decimal places use the function

    Code:
    roundNumber = function(num, dec) {
    return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    }
    
    e.g. roundNumber(123.45678,2);



    Use Number() in place of parseInt() or parseFloat()

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 09-10-2012 at 08:46 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    greenhat (09-11-2012)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Awesome

    That worked out terrific. Thanks a bunch.


  •  

    Posting Permissions

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