javascript for 循环在输出中产生错误

标签 javascript for-loop counter

下面的 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/

相关文章:

php - 无法使 "Reload"脚本在 PHP 中工作

javascript - 使用 jquery 向 HTML 添加一个类?

javascript - 为什么我的数组中有空项目,我该如何摆脱它们?

javascript - 函数式编程 - 递增计数器的简单 For 循环

python - 添加像计数器这样的 Numpy 数组

javascript - 谷歌 JavaScript 验证码处理

javascript - 通过超时在 Javascript 中休眠?

php - 使变量变量可在循环外访问

javascript - 如何使用for循环从父节点中删除子节点(html div)

c# - 每个结果带有计数器的匿名类型