如何在javascript函数中的任意位置使用变量(值)
下面是我的javascript函数的一部分。在该函数中,我尝试使用数学公式:如何在javascript函数中的任意位置使用变量(值),javascript,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 be
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作为全局变量进行初始化