我正在创建一个小型网络应用程序,用户可以输入他们理想的汽车价格并了解他们每月的付款金额(平均)。
该函数的一半有效,并且能够通过计算获得 topHalf 变量,但不能获得方程的其余部分。
使用的公式
http://teachertech.rice.edu/Participants/bchristo/lessons/carpaymt.html
已使用 HTML
<input type="input" placeholder="carPrice" id="rPrice">
<input type="input" placeholder="deposit" id="deposit">
<input type="input" placeholder="interest rate" id="iRate">
<input type="input" placeholder="loan term" id="tMonth">
<a onclick="findPayment()">Calculate</a>
使用 JavaScript
function findPayment() {
/* Add User's unique input fields here */
var regPrice = document.getElementById("rPrice").value;
deposit = document.getElementById("deposit").value;
rate = document.getElementById("iRate").value;
mth = document.getElementById("tMonth").value;
/* Find updated price including user's deposit */
uptPrice = regPrice - deposit;
/* Formula to Calculate Here */
topHalf = uptPrice * ( rate / mth );
bottomHalf = 1 - Math.pow( 1 + rate / 12, -mth)
custom = topHalf / bottomHalf;
/* Send monthly payment to console so I can check */
console.log(custom);
};
感谢您的帮助!
最佳答案
您需要parseInt
或parseFloat
实际输入的值以将它们作为数字进行操作。
目前它应该显示 NaN 或类似的东西,NaN 代表不是数字,它表明因为您正在尝试在字符串上使用数学运算符。
关于javascript - 为什么我计算每月汽车付款的功能不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447131/