我打算让下面的函数更新变量,然后将结果显示在DOM中,但变量ldead继续显示为0,这里有什么问题?
cgr();
var ldead = 0
var total_customer = 0;
function cgr() {
for (var year = 0; (churn * total_customer) < (leadr * closing); year++) {
total_customer = total_customer + (leadr * closing);
if ((churn * total_customer) >= (leadr * closing)) {
var ldead = year;
}else {
$("#car").html("something is wrong");
};
}
}
完整的脚本在这里:http://pastebin.com/b0JqhTnB
答案 0 :(得分:1)
您正在重新声明要为其指定值的变量ldead。
删除&#34; var&#34;在if语句中的变量名前面,如下所示:
cgr();
var ldead = 0
var total_customer = 0;
function cgr() {
for (var year = 0; (churn * total_customer) < (leadr * closing); year++) {
total_customer = total_customer + (leadr * closing);
if ((churn * total_customer) >= (leadr * closing)) {
ldead = year;
}else {
$("#car").html("something is wrong");
};
}
}
答案 1 :(得分:0)
var
,因此您创建了一个新的本地范围变量,而不是更新范围内的变量。删除那里的var
。答案 2 :(得分:0)
leadr
未设置,因此ldead
永远不会被分配0以外的任何内容。