下面的 for 循环在 html 输出中产生错误,我用其他函数替换,并且 html 输出很好,所以我确信它是这个函数。我还在 ide 中重新创建了它,替换 $("#car").html(year);对于console.log(年份);它产生了正确的输出。这个函数有什么问题吗?
function cgr() {
for (var year = 0; (churn * total_customer) < leadcon; year++) {
total_customer = total_customer + leadcon;
if ((churn * total_customer) >= leadcon) {
$("#car").html(year);
}else {
$("#car").html("something is wrong");
};
}
}
完整脚本可以在这里找到:http://pastebin.com/DcDKNfux
最佳答案
循环条件 (churn * Total_customer) < Leadcon 似乎与循环计数器“year”无关,循环计数器“year”初始化为零,然后递增。变量 churn、total_customer 和 Leadcon 在哪里初始化,初始化为什么值?
无论如何,当进入循环迭代时,循环条件 (churn * Total_customer) < Leadcon 为 true 是完全合乎逻辑的,然后 if 条件 (churn * Total_customer) >= Leadcon) 也为 true在使用total_customer += Leadcon 修改total_customer 变量后为真。
但是很难知道预期结果是什么,尤其是在没有有关输入数据的更多信息的情况下。
我会在函数中添加更多日志记录,以便您可以跟踪正在发生的事情,如下所示:
function cgr() {
for (var year = 0; (churn * total_customer) < leadcon; year++) {
$("#car").html("entering loop, year=" + year + ", churn=" + churn + ", total_customer=" + total_customer + ", leadcon=" + leadcon);
total_customer = total_customer + leadcon;
$("#car").html("after mod, total_customer=" + total_customer);
if ((churn * total_customer) >= leadcon) {
$("#car").html(year);
}else {
$("#car").html("something is wrong");
}
}
关于javascript for 循环在输出中产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23686557/