Failure to calculate the total monthly payment for a car loan. What's going wrong?

215 views Asked by At

Updated code. calculate() still not working. The monthly payment amount is ultimately not being passed to the "total" id box. Does anyone see what the underlying problem is here? My syntax seems to be correct, I believe there may be a problem with my specification of where each variable is applied in the code.

I am having problems getting the function calculate() to pass the correct result in "total." Any possible solutions? The action of clicking the calculate button should display the total, but is displaying nothing at all, as if the button does not activate the calculate function at all.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
function calculate() {
var P = document.getElementById("price").value;
var D = document.getElementById("DP").value;
var R = document.getElementById("R").value;
var N = document.getElementById("N").value;
var i = (R / 1200);
var n = (N * 12);
var m = ((P - D) * i * Math.pow(1 + i,n)) / (Math.pow(1 + i,n) - 1);
var result = document.getElementById('total');
    result.value = m;}
</script>
</head>
<div align="center">
    <hr>
    <form name id="Main">
        <input type="number" id="price" placeholder="Price of the Car"/>
        <br>
        <br>
        <input type="number" id="DP" placeholder="Down Payment"/>
        <br>
        <br>
        <input type="number" id="R" placeholder="Annual % Rate"/>
        <br>
        <br>
        <input type="number" id="N" placeholder="# of Years Loaned"/>
        <br>
        <br>
        <input type="button" id="calculate" value="Calculate" onclick="javascript:calculate();"/>
        <br>
        <br>
        <input type="number" id="total" placeholder="Total Cost..." readonly=""/>
        <br>
        <br>
        <input type="reset" value="Reset">
    </form>
    <hr>
</div>
</html>
3

There are 3 answers

0
lizardwizard On BEST ANSWER
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function calculate() {
    var P = document.getElementById("price").value;
    var D = document.getElementById("DP").value;
    var R = document.getElementById("R").value;
    var N = document.getElementById("N").value;
    var i = (R / 1200);
    var n = (N * 12);
    var m = ((P - D) * i * Math.pow(1 + i,n)) / (Math.pow(1 + i,n) - 1);
    var result = document.getElementById('total');
        result.value = m;}
</script>
</head>
<div align="center">
<hr/>
<form id="Main">
    <input type="number" id="price" placeholder="Price of the Car" />
    <br />
    <br/>
    <input type="number" id="DP" placeholder="Down Payment" />
    <br/>
    <br/>
    <input type="number" id="R" placeholder="Annual % Rate" />
    <br/>
    <br/>
    <input type="number" id="N" placeholder="# of Years Loaned" />
    <br/>
    <br/>
    <input type="button" value="Calculate Monthly Payment" onclick="calculate();" />
    <br/>
    <br/>
    Total Monthly Payment:<input type="number" id="total" placeholder="Total Cost..." readonly="" />
    <br/>
    <br/>
    <input type="reset" value="Reset" />
</form>
<hr/>
</div>
19
jotadepicas On

Use Math.pow() function instead. The ^ operator is not for mathematical power operations (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow).

Also, It's result.value = m, not the other way around :)

Also 2: R and M seem undefined to me, you have to initialize those variables with something, like you did with P and D.

Also 3: use chrome dev tools or anything like that. It will make your life much easier. Remember, javascript doesn't mean "no IDE" :D

8
vitr On

Unfortunately, your original code will never work, as it has too many errors.
Here is the corrected fiddle for you, please, have a look: http://jsfiddle.net/q330fqw8/

function calculate() {
  var P = document.getElementById("price").value;
  var D = document.getElementById("DP").value;
  var R = document.getElementById("R").value;
  var N = document.getElementById("N").value;
  var i = (R / 1200);
  var n = (N * 12);
  var m = ((P - D) * i * Math.pow(1 + i,n)) / (Math.pow(1 + i,n) - 1);
  var result = document.getElementById('total');
  result.value = m;
}

I removed id="calculate" in HTML, because of this: Why JS function name conflicts with element ID?
In Javascript, the 4 variables P,D,R,N should be set properly. Finally, this m.value = result; -> result.value = m;
but I guess, you've already corrected some errors in the question. I worked with your original code.