复利

203 阅读1分钟
/**
init: 初始金额
years: 定投年限
rate: 年化收益率
money: 每月定投金额
totalYears: 投入总年限
*/
function cal({init, years, rate, money, totalYears}){
    let total = init;
    let count = 1;
    while(count<=years){
      let inMoney = money * 12;
      let outMoney = (total+ inMoney) * rate;
      total = total + inMoney + outMoney;
      console.log(`定投第${count}年: ${total}`);
      count++;
    }
    while(count<=totalYears){
      total = total * (1+rate)
      console.log(`复利第${count}年: ${total}`);
      count++
    }
    console.log(`总投入: ${init + years * money * 12}`)
    console.log(`总收入: ${total}`)
    console.log(`收入/投入: ${total/(init+years* money * 12)}`)
    return total;
  }
// cal({init:60000, years: 10, rate: 0.12, money: 1000, totalYears: 30})
// 总投入: 180000
// 总收入: 4072721
// 收入/投入: 22.6