0

I really need some help with this one. The function is supposed to calculate the estimated closing costs. But for some reason the result is always "NaN" whatever I do with it. Can anyone help me out? I have no idea where it goes wrong.

function calc_closing_costs(form) {
    var purch_price = document.getElementById('purch_price');
    var ont_ltt = 0;
    if ((isNaN(parseInt(purch_price.value, 10))) || purch_price === "") {
        alert("You must enter a number!");
    } else if (parseInt(purch_price.value, 10) < 0) {
        alert("You must enter a positive number!");
    }
    if (400000 < parseInt(purch_price.value, 10)) {
        ont_ltt = (parseInt(purch_price.value, 10) - 400000) * 0.02 + 4475;
    } else {
        if (250000 < parseInt(purch_price.value, 10)) {
            ont_ltt = (parseInt(purch_price.value, 10) - 250000) * 0.015 + 2225;
        } else {
            if (55000 < parseInt(purch_price.value, 10)) {
                ont_ltt = (parseInt(purch_price.value, 10) - 55000) * 0.01 + 275;
            } else {
                ont_ltt = parseInt(purch_price.value, 10) * 0.005;
            }
        }
    }
    var legal_fees = document.getElementById('legal_fees');
    var closing_adj = document.getElementById('closing_adj');
    var result = document.getElementById('result');

    if (purch_price.value === "" || purch_price.value != parseFloat(purch_price.value)) {
        purch_price.value = 0;
    }
    if (legal_fees.value === "" || legal_fees.value != parseFloat(legal_fees.value)) {
        legal_fees.value = 0;
    }
    if (closing_adj.value === "" || closing_adj.value != parseFloat(closing_adj.value)) {
        closing_adj.value = 0;
    }
    result.value = 0;
    result.value = parseInt(result.value, 10);
    result.value = parseInt(ont_ltt.value, 10) + parseInt(legal_fees.value, 10) + parseInt(closing_adj.value, 10);
    document.getElementById("tecc").innerHTML = result.value;
}
1
  • 3
    Your code shouldn't keep calling parseInt() on the same thing over and over and over again. Call it once and save the result. Commented Dec 1, 2013 at 19:19

1 Answer 1

2

This:

    result.value = parseInt(ont_ltt.value, 10) + parseInt(legal_fees.value, 10) + parseInt(closing_adj.value, 10);

doesn't make sense — "ont_ltt" is a JavaScript variable, not a page element.

It should just be:

     result.value = ont_ltt + parseInt(legal_fees.value, 10) + parseInt(closing_adj.value, 10);

Well, maybe it should; you didn't post the page or the overall plan for what you're trying to do. Using parseInt() in particular seems questionable. It will always give you an integer and truncate any fractional part of the value (anything after a decimal point).

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, Pointy! Thanks for your response. I removed the unnecessary parseInt from the code (and by unnecessary I mean I removed them one by one and checked if the script was still working, I'm a newbie as you can see) and putting ont_ltt instead of parseInt(ont_ltt.value,10) helped. It finally gives me a number! Thanks a lot! I really appreciate your help.

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.