简体   繁体   English

Javascript - 你如何找到一个数字随时间的总增长?

[英]Javascript - How do you find the total increase of a number over time?

Thanks for stopping by!感谢您的光临!

I am creating a small program that will display the total compound interest.我正在创建一个小程序,它将显示总复利。
For example, let's say every month a person were to get 5% increase on their monthly income.例如,假设每个月一个人的月收入增加 5%。 They start off with $1,000, but after a year, they would be making $1,820 per month.他们从 1,000 美元开始,但一年后,他们将每月赚 1,820 美元。

I got it to the point where it will display the monthly income after the 1 year ($1,820), but I can't seem to find a way to get it to display the total increase you made over the 1 year.我已经知道它会显示 1 年后的月收入(1,820 美元),但我似乎无法找到一种方法让它显示你在 1 年内所做的总增加。

Month 1 = +50第 1 个月 = +50
Month 2 = +104第 2 个月 = +104
Month 3 = +160第 3 个月 = +160
Month 4 = +220第 4 个月 = +220
Month 5 = +282第 5 个月 = +282
Month 6 = +348第 6 个月 = +348
Month 7 = +417第 7 个月 = +417
Month 8 = +490第 8 个月 = +490
Month 9 = +566第 9 个月 = +566
Month 10 = +647第 10 个月 = +647
Month 11 = +731第 11 个月 = +731
Month 12 = +820第 12 个月 = +820
________________ ________________
Total = $4,835总计 = 4,835 美元

Normally this would be very easy to do, but since the amount, percentage, and duration are all variables, I am at a loss.通常这很容易做到,但由于金额、百分比和持续时间都是变量,我不知所措。

Basically what I am looking to do would be基本上我想做的是

"Amount: 1000 "数量:1000
Percentage: 5百分比:5
Duration: 12持续时间:12

Total: $1,820总计:1,820 美元
Total Money Gained: $4,835"总收入:$4,835"

Thank you so much for taking the time to help!非常感谢您抽出宝贵时间提供帮助!
Have a fantastic rest of your day!度过美好的一天!

HTML HTML

<input id="amount" type="number" onchange="myFunction()">
<input id="percentage" type="number" onchange="myFunction()">
<input id="duration" type="number" onchange="myFunction()">
<h2 id="payment"></h2>

JAVASCRIPT爪哇脚本

function myFunction() {
    var amount = document.getElementById('amount').value;
    var percentage = document.getElementById('percentage').value;
    var duration = document.getElementById('duration').value;

    var payment = Math.round(amount * (1 + (percentage * 0.01) / duration)**(duration**2));

    document.getElementById('payment').textContent=payment;
}

CodePen Example 代码笔示例

You need to loop through each tick in duration , calculate the difference from the base amount, and print it:您需要遍历duration每个刻度,计算与基本金额的差值,然后打印出来:

 const outputElm = document.getElementById("payment"); const appendP = textContent => { outputElm.appendChild(document.createElement('p')) .textContent = textContent; }; function somethingHere() { outputElm.textContent=""; var base = document.getElementById('amount').value; var percentage = document.getElementById('percentage').value; var duration = document.getElementById('duration').value; const multiplier = 1 + (percentage*0.01); let payment = base; for (let i = 0; i < duration; i++) { payment *= multiplier; appendP(`Month ${i + 1}: +$${Math.round(payment - base)}`); } appendP('----------------'); appendP(`Total: $${Math.round(payment)}`); }
 <p>Amount <input id="amount" type="number" value="0" onchange="somethingHere()"></p> <p>Percentage <input id="percentage" value="5" type="number" onchange="somethingHere()"></p> <p>Duration <input id="duration" type="number" value="12" onchange="somethingHere()"></p> <div id="payment"></div>

To begin with, your compound interest calculation is wrong.首先,您的复利计算是错误的。 Using the variables a (amount), i (increase) and d (duration), the compounded amount would be expressed as:使用变量a (金额)、 i (增加)和d (持续时间),复合金额将表示为:

复合金额

Then, for the total amount gained over the duration, the sum expression would be:然后,对于在持续时间内获得的总金额,总和表达式将是:

求和表达式

Which can be rewritten as:可以改写为:

总和改写

Plugging that into the JavaScript code, you get the following:将其插入 JavaScript 代码,您将得到以下内容:

 function myFunction() { var a = +document.getElementById('amount').value; var i = +document.getElementById('percentage').value / 100; var d = +document.getElementById('duration').value; var payment = a * ((1 + i) ** d); var total = a / i * (((1 + i) ** d) + i * ((1 + i) ** d - d - 1) - 1); document.getElementById('payment').textContent = payment; document.getElementById('total').textContent = total; }
 <input id="amount" type="number" onchange="myFunction()"> <input id="percentage" type="number" onchange="myFunction()"> <input id="duration" type="number" onchange="myFunction()"> <h2 id="payment"></h2> <h2 id="total"></h2>

use looping to determine the increase every month.使用循环来确定每个月的增加。 After getting the increase, add it to the total number of increase.得到增量后,将其添加到增量总数中。

 function increase() { var amount = document.getElementById('amount').value; var percentage = document.getElementById('percentage').value; var duration = document.getElementById('duration').value; var total = 0; for (var i = 1; i <= duration; i++) { var payment = Math.round(amount * (1 + (percentage * 0.01) / i) ** (i ** 2)); total += (payment - amount); } document.getElementById('payment').textContent = payment; console.log(total); }
 <p>Amount <input id="amount" type="number" value="0" onchange="increase()"></p> <p>Percentage <input id="percentage" value="5" type="number" onchange="increase()"></p> <p>Duration <input id="duration" type="number" value="12" onchange="increase()"></p> <div id="payment"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM