简体   繁体   中英

How to use variable(value) anywhere in function in javascript

Below there if part of my javascript function. In that function, i am trying to use mathematical formula:

if(m == 1){
     var intAmt = amt * (Math.pow((1 +  rate / (12*100)), 12*period)) - amt;
     var notPaid = Math.round(parseInt(amt) + intAmt,2);
}else if(status === "Not Paid"){


//dueAmt is tobe taken from below
     var intAmt = dueAmt * (Math.pow((1 +  rate / (12*100)), 12*period)) -dueAmt;
     var notPaid =  parseInt(dueAmt) + parseInt(amt) + intAmt;
}
//dueAmt is passed to the formula again n again
var dueAmt = notPaid; 

the above part of code is javascript function is in for loop

I need to use the dueAmt variable again in if loop.But in else part of the loop it gives me undefined.

Try to use dueAmt as a global variable instead of local var.

And suppose your else block execute first then it definitively gives undefined because you never assigned value to dueAmt and started multiplying.

So initialize with 0 or 1 as a global variable according to need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM