gpt4 book ai didi

Javascript foreach(锻炼兴趣)

转载 作者:行者123 更新时间:2023-11-30 21:20:48 24 4
gpt4 key购买 nike

我正在尝试计算输入值的兴趣。

我要求提供原则利率、百分比利率和年数。

我想弄清楚他们的simpleInterest和compundInterest。

我希望它在表格中显示每年的结果值。

我有一个表格,当我输入数量时,它会获取年数,例如 15。当我输入 15 时,它显示 15 行,但它们都是一样的。

我需要它来计算每一行每年的值。

我当前的代码只显示 15 年的相同结果。

我的代码:

<div class="container">
<div id="content">
<h2 class="interestOutput">Table for $<span id="interestPrinciple">Loading...</span> at <span id="interestRate">Loading...</span>%
</h2>
<table>
<tr>
<th>Year</th>
<th>SimpleInterest</th>
<th>CompoundInterest</th>
</tr>
<div id="tableResult">

</div>
</table>
</div>

<script type="text/javascript">
if (document.readyState = "complete") {
var principle = localStorage.getItem("principle");
var rate = localStorage.getItem("rate");
var years = localStorage.getItem("years");

var simple_interest = principle * (rate / 100) * years;
var compound_interest = principle * (1 + rate/100);

/*
var list = computeSchedule(p, rate, 12, y, monthlyPayment);
var tables = "";
for (var i = 0; i < list.length; i++) {
tables += "<tr>" +
"<td>" + list[i][0] + "</td>" +
"<td>" + list[i][1] + "</td>" +
"<td>" + list[i][2] + "</td>" +
"<td>" + list[i][3] + "</td>" +
"<td>" + list[i][4] + "</td>" +
"</tr>";
}
document.getElementById("demo").innerHTML = '<table>' + tables + '</table>';
*/

var tables = "";
for (var i = 0; i < years; i++) {
tables +=
"<tr>" +
"<td>" + years + "</td>" +
"<td>" + simple_interest + "</td>" +
"<td>" + compound_interest + "</td>"+
"</tr>";
}
document.getElementById("tableResult").innerHTML = '<table>' + tables + '</table>';

document.getElementById("interestPrinciple").innerHTML = principle;
document.getElementById("interestRate").innerHTML = rate;
//document.getElementById("gradeOutput").innerHTML = years;
}
</script>

I have now sorted the simple interest column but the compound column is not correct.

我得到了以下信息:计算单利的公式是利息 = 本金 * (利率/100) * 年。只需将本金添加到利息中即可生成要显示的金额。计算复利的公式是 FinalAmount = principal * (1 + rate/100)Years。请注意,在这种情况下您不需要添加委托(delegate)人。您的程序必须读取本金金额、利率 (%) 和最长年限(请参阅下面的网站快照/视频)。使用以下消息读取适当的值:

我更新的代码是:

<div class="container">
<div id="content">
<h2 class="interestOutput">Table for $<span id="interestPrinciple">Loading...</span> at <span id="interestRate">Loading...</span>%
</h2>
<div id="tableResult"></div>
</div>
</div>

<script type="text/javascript">
if (document.readyState = "complete") {
var principle = localStorage.getItem("principle");
var rate = localStorage.getItem("rate");
var years = localStorage.getItem("years");
var tables = "";
for (var i = 0; i < years; i++) {

var simple_interest = principle * (rate / 100) * i;
var compound_interest = principle * (1 + (rate/100));

var final_simple = compound_interest + simple_interest;
var add_extra = 1 + (rate/100);
var final_compound = final_simple + add_extra;

tables +=
"<tr>" +
"<td>" + (i + 1) + "</td>" +
"<td>$" + final_simple + "</td>" +
"<td>$" + final_compound + "</td>"+
"</tr>";
}
document.getElementById("tableResult").innerHTML = '<table><tr><th>Year</th><th>SimpleInterest</th><th>CompoundInterest</th></tr>' + tables + '</table>';

document.getElementById("interestPrinciple").innerHTML = principle;
document.getElementById("interestRate").innerHTML = rate;
}
</script>

最佳答案

据我所知,多年来您的迭代没有调用问题解决函数。您的变量使用可用数据声明一次,并且您确实在相同的数据年内循环了很多次。

尝试这样的事情。

var tables, principle, years, rate, add_extra, times_compunded_per_tear, compound_rate, i;

principle = 10000;
years = 6;
rate = 5.6;
tables = '';
times_compounded_per_year = 12
compound_rate = 1 / times_compounded_per_year;

function simple_interest(currentYear) {

return principle * (rate / 100) * currentYear + principle;

}

function compound_interest(currentYear) {

return principle * (Math.pow(((1 + rate/100) / compound_rate), (compound_rate * currentYear)));

}

for (i = 0; i < years + 1; i++) {

tables +=
"<tr>" +
"<td>Year: "+ i +"</td>" +
"<td>Simple Interest "+ simple_interest(i).toFixed(2) + "</td>" +
"<td>Compound Interest "+ compound_interest(i).toFixed(2) + "</td>" +
"</tr>";
}
document.getElementById("tableResult").innerHTML = '<table>' + tables + '</table>';

document.getElementById("interestPrinciple").innerHTML = "Principle: "+principle;
document.getElementById("interestRate").innerHTML = "Interest Rate: "+rate;
//document.getElementById("gradeOutput").innerHTML = years;
<ul>
<li id="interestPrinciple"></li>
<li id="interestRate"></li>
</ul>
<div id="tableResult"></div>

关于Javascript foreach(锻炼兴趣),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45201763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com