我正在使用一个计算器来计算单利和复利。很好,仅凭简单的兴趣,但我似乎无法使用循环来解决复利问题。我需要一个循环,因为将数据推入数组以供以后在图表中使用。
我从这里得到公式:https://www.moneysmart.gov.au/managing-your-money/saving/compound-interest
我将其用作参考:https://www.bankrate.com/calculators/retirement/roi-calculator.aspx
代码在这里起作用:http://www.course0001.com/fiverr/iddqd 到目前为止,我有这个(更新):
// Inputs from user:
// Initial deposit (starting balance)
// Number of years
// Interest
// Frequent Deposit amount
// Deposit and compound frequency (monthly, weekly, yearly)
// Calculations
var investedCapitalArray = [];
var simpleInterestArray = [];
var compoundInterestArray = [];
var compoundPrincipal = 0;
var years = [];
var accumulatedInvestment;
function calculate() {
years = [];
let interest = rateNumeric.getNumber() / 100; // annual interest rate
let additionalDeposit = additionalNumeric.getNumber(); // Regular deposit
let frequency = freqInput.value; // Frequency of regular deposit
let initialDeposit = initialNumeric.getNumber();
let taxRate = taxNumeric.getNumber();
// Invested captal amount first year
investedCapitalArray = [];
investedCapitalArray.push(initialDeposit + (frequency * additionalDeposit));
// simple interest first year
simpleInterestArray = [];
simpleInterestArray.push((investedCapitalArray[0] * ( (interest) / 100)) * (1 - taxRate));
// compund interest first year
compoundInterestArray = [];
let firstYearInvestment = investedCapitalArray[0]; // First deposit + regular deposits in first year
for (let i = 1 ; i < yearsInput.value ; i++) {
// Invested capital over the years (correct results)
investedCapitalArray.push( (investedCapitalArray[i-1]) +
(frequency * additionalDeposit) );
// simple interest over the years (correct results)
simpleInterestArray.push( simpleInterestArray[i-1] +
((firstYearInvestment +
((frequency) * additionalDeposit) * i ) * interest) );
// compound interest over the years (incorrect results)
compoundInterestArray.push( investedCapitalArray[i-1] *
Math.pow(1 + interest / 100, i) - initialDeposit);
years.push('Year' +i);
}
}
答案 0 :(得分:0)
问题在于您应该使用(investedCapitalArray[i - 1] + compoundInterestArray[i - 1]) * (1 + 0.07)
的括号。谢谢
答案 1 :(得分:0)
我认为问题出在将对象拆箱。试试这个:
compoundInterestArray.push( compoundInterestArray[i-1] + (parseInt(investedCapitalArray[i-1]) + parseInt(simpleInterestArray[i-1])) * ( rateNumberic.getNumber() / 100)) );
答案 2 :(得分:0)
感谢大家的投入,在深入研究复利主题之后,我编写了一种效果很好的算法。实际上很简单。
我的算法基于以下解释: “什么是复利?复利(或复利)是根据初始本金计算的利息,还包括存款或贷款以前各期的所有累计利息。”
因此它像这样在循环中工作:
compoundInterest + =((((simpleInterestArray [i-1] + compoundInterest)*(兴趣));
下面的完整代码。
for (let i = 1; i < yearsInput.value; i++) {
// Invested capital over the years (works fine)
investedCapital = (investedCapitalArray[i - 1]) +
(frequency * additionalDeposit);
investedCapitalArray.push(investedCapital);
// imple interest over the years (works fine)
simpleInterest = simpleInterestArray[i - 1] +
((firstYearInvestment + (frequency * additionalDeposit) * i) * interest);
simpleInterestArray.push(simpleInterest);
// compound interest over the years (correct results)
compoundInterest += (((simpleInterestArray[i - 1] + compoundInterest) * (interest)));
compoundInterestArray.push(compoundInterest);
}