Javascript-您如何找到一个数字随时间增加的总数? [英] Javascript - How do you find the total increase of a number over time?

查看:35
本文介绍了Javascript-您如何找到一个数字随时间增加的总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的光临!

我正在创建一个小程序,将显示总复利.
例如,假设一个人每个月的月收入增加5%.他们以1,000美元开始,但是一年后,他们每个月的收入为1,820美元.

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. They start off with $1,000, but after a year, they would be making $1,820 per month.

我已经知道它会显示 1 年后的月收入(1,820 美元),但我似乎无法找到一种方法让它显示您在 1 年内所做的总增长.

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个月= +50
第二个月= +104
第3个月= +160
第4个月= +220
第5个月= +282
第6个月= +348
第7个月= +417
第8个月= +490
第9个月= +566
第10个月= +647
第11个月= +731
12个月= +820
________________
总计= $ 4,835

Month 1 = +50
Month 2 = +104
Month 3 = +160
Month 4 = +220
Month 5 = +282
Month 6 = +348
Month 7 = +417
Month 8 = +490
Month 9 = +566
Month 10 = +647
Month 11 = +731
Month 12 = +820
________________
Total = $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.

基本上我想做的是

金额:1000
百分比:5
持续时间:12

总计:$ 1,820
总收益:4,835美元"

"Amount: 1000
Percentage: 5
Duration: 12

Total: $1,820
Total Money Gained: $4,835"

非常感谢您抽出宝贵的时间来提供帮助!
祝您有美好的一天!

Thank you so much for taking the time to help!
Have a fantastic rest of your day!

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 示例

推荐答案

使用循环来确定每个月的增长.得到增加后,将其添加到增加的总数中.

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>

这篇关于Javascript-您如何找到一个数字随时间增加的总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆