0

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. I also recreated it in an ide substituting $("#car").html(year); for 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

6
  • 2
    What error? What was expected that this doesn't do? What other function did you use that worked? Commented May 15, 2014 at 19:00
  • you are changing html of $("#car") in a for loop without a break; statement after success !(why?) . Commented May 15, 2014 at 19:04
  • What would the break do? If the condition is met the loop stops. Commented May 15, 2014 at 20:20
  • It seems this error is being triggered: function ShowValue (selector, v) { $(selector).html(isNaN(v) ? "Error" : "$" + Round2Cent(v)); } Commented May 15, 2014 at 20:21
  • Sure, when you call the $("#car").html() function with a string ("something is wrong") instead of a number, then isNaN() for the string returns true, and that function just outputs "Error." I think that's not really directly related to your question, though, which is why you get to the "something is wrong" part of the code, right? Commented May 16, 2014 at 1:08

1 Answer 1

1

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. Where are the variables churn, total_customer, and leadcon initialized, and to what values?

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.

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");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

The variables are defined in the script
Yes, they're declared in the script, but I don't know what values they have, nor am I in a position to debug your entire client-side script. If you log the values like I suggested, then it will be more obvious what they are, and presumably why they're being handled in a way different from what you expect.
The values are entered by the user in the fields of the DOM.
Sure, but again, I don't have any way of testing the function without hard-coding the parameters. What values are you using which cause the failure? Again, logging that stuff somehow, even if you can't log a string with the $("#car").html function, would go a long way to debugging this.
See this link for the UI, the function should generate the result for the field "Customer Acquisition Rate". I have tested it many different ways but can not get it to output. jsfiddle.net/RT6Yn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.