简体   繁体   English

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

[英]javascript for loop produces error in output

The below for loop produces an error in the html output, I substituted with other function and the html output was fine, so I'm convinced its this function. 下面的for循环在html输出中产生错误,我用其他函数替换了,并且html输出很好,所以我确信它是这个函数。 I also recreated it in an ide substituting $("#car").html(year); 我还用一个$(“#car”)。html(year);的ide重新创建了它。 for console.log(year); 对于console.log(year); and it produced the correct output. 它产生了正确的输出。 What is the problem with this function? 此功能有什么问题?

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");
    };
}

} }

full script can be found here: http://pastebin.com/DcDKNfux 完整的脚本可以在这里找到: http : //pastebin.com/DcDKNfux

The loop condition, (churn * total_customer) < leadcon, seems to have no relation to the loop counter "year" which is initialized at zero and then incremented. 循环条件(churn * total_customer)<Leadcon,似乎与循环计数器“ year”没有关系,循环计数器“ year”被初始化为零,然后递增。 Where are the variables churn, total_customer, and leadcon initialized, and to what values? 变量churn,total_customer和Leadcon的初始化位置在哪里,以及什么值?

In any case, it's completely logical for the loop condition (churn * total_customer) < leadcon to be true when the iteration of the loop is entered, and then for the if condition (churn * total_customer) >= leadcon) to also be true after the total_customer variable is modified with essentially total_customer += leadcon. 无论如何,在进入循环的迭代时,使循环条件(churn * total_customer)<leadcon为真,然后使if条件(churn * total_customer)> = leadcon)为真,这是完全合乎逻辑的total_customer变量实际上是使用total_customer + = Leadcon进行修改的。

But it's hard to know what the expected results are, especially without more information about the input data. 但是很难知道预期的结果是什么,尤其是在没有更多有关输入数据的信息的情况下。

I would add more logging in the function so that you can trace what's happening, something like this: 我将在函数中添加更多日志记录,以便您可以跟踪正在发生的事情,如下所示:

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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM