ปัญหา
เรามีจำนวนเงินต้น amt > 0 และฝากด้วยอัตราดอกเบี้ย p เปอร์เซ็นต์ หาร 360 ต่อวัน เริ่มวันที่ 1 มกราคม 2021 เราต้องการให้มีจำนวนเงินรวม total >= a0
ฟังก์ชันควรรับพารามิเตอร์ 3 ตัวนี้ และคืนคาวันที่ที่จำนวนเงินจะเท่ากับจำนวนที่ต้องการ
ตัวอย่าง
โค้ดตัวอย่างดังนี้:
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