CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   How do you add values from separate functions? (http://www.codingforums.com/showthread.php?t=272778)

greenhat 09-10-2012 07:57 PM

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>

Philip M 09-10-2012 08:35 PM

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.

greenhat 09-10-2012 11:48 PM

Awesome
 
That worked out terrific. Thanks a bunch.


All times are GMT +1. The time now is 08:15 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.