这就是我要编写JavaScript的目的。为了解决这些年,需要花费“ X”美元来退休。从逻辑上讲,这可以通过财务计算器或反复试验的未来价值方程来解决。有没有更清洁的方法来解决这个问题?
html>
<head>
<title></title>
</head>
<script>
function calculate()
{
ra = document.getElementById("ra").value; // amount needed
to retire
p = document.getElementById("n").value; // initial
investment
t = document.getElementById("t").value; // annual
contribution
r = document.getElementById("rate").value; // radio button
interest rate
}
</script>
<body>
<h1>You probably want to retire someday so let's get started</h1>
<br>
<div> <label> How much money do you need to retire </label> <input
id="ra"> </div> <br>
<div> <label>Initial Investment </label> <input id="p"> </div>
<br>
<div> <label>Annual Contribution: </label> <input id="ac"> </div>
<br>
Expected Interest Rate: Select from the following please: <br>
<input type="radio" name="rate" value="1"> 1 <br>
<input type="radio" name="rate" value="1.5"> 1.5<br>
<input type="radio" name="rate" value="2"> 2 <br>
<input type="radio" name="rate" value="2.5"> 2.5<br>
<input type="radio" name="rate" value="3"> 3 <br>
<input type="radio" name="rate" value="3.5"> 3.5<br>
<input type="radio" name="rate" value="4"> 4 <br>
<input type="radio" name="rate" value="4.5"> 4.5<br>
<input type="radio" name="rate" value="5"> 5 <br>
<input type="radio" name="rate" value="5.5"> 5.5<br>
<input type="radio" name="rate" value="6"> 6 <br>
<input type="radio" name="rate" value="6.5"> 6.5<br>
<button onclick="calculate()">Calculate</button>
<input type="button" value="Refresh Page"
onClick="window.location.reload()">
<p id="result"> </p>
display how many years it would take here
</body>
</html>
答案 0 :(得分:0)
由于此情况将应用复利,
Time (t) = ln(A/P) / r
where, A = final Amount
P = Principal
r = Interest Rate(R)/100
针对您的情况-
document.getElementById("result).value = Math.log(ra/p)*100/r