簡體   English   中英

如何在javascript中的函數中的任何地方使用變量(值)

[英]How to use variable(value) anywhere in function in javascript

下面是我的 javascript 函數的一部分。 在那個函數中,我試圖使用數學公式:

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; 

上面部分代碼是javascript函數在for循環中

我需要在 if 循環中再次使用 DueAmt 變量。但在循環的其他部分,它給我未定義。

嘗試使用 DueAmt 作為全局變量而不是局部變量。

並假設您的 else 塊首先執行,然后它明確給出 undefined 因為您從未將值分配給 DueAmt 並開始乘法。

所以根據需要初始化為0或1作為全局變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM