在 JavaScript 中查詢存款金額等於特定金額的日期


問題

我們有一筆金額 amt > 0,我們將其存入賬戶,年利率為 p%,每天除以 360,起始日期為 2021 年 1 月 1 日。我們希望賬戶金額達到 total >= a0。

我們的函式應該接受這三個引數,並返回金額等於目標金額的日期。

示例

以下是程式碼:

 即時演示

const principal = 100;
const amount = 150;
const interest = 2;
const findDate = (principal, amount, interest) => {
   const startingDate = new Date('2021-01-01')
   const dailyInterestRate = interest / 36000
   let startingMoney = principal
   let daysPassed = 0
   while (startingMoney < amount) {
      daysPassed++
      startingMoney += startingMoney * dailyInterestRate
   };
   startingDate.setDate(startingDate.getDate() + daysPassed)
   return startingDate.toISOString().split('T')[0]
};
console.log(findDate(principal, amount, interest));

輸出

2040-12-26

更新於:2021-04-17

82 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.