Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript函数中的任意位置使用变量(值)_Javascript - Fatal编程技术网

如何在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

下面是我的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作为全局变量进行初始化