Vấn đề
Chúng tôi có một số tiền> 0 và chúng tôi gửi nó với lãi suất p phần trăm chia cho 360 mỗi ngày vào ngày 1 tháng 1 năm 2021. Chúng tôi muốn có tổng số tiền> =a0.
Hàm của chúng ta sẽ nhận ba tham số này và trả về ngày mà số tiền sẽ bằng số tiền mong muốn
Ví dụ
Sau đây là mã -
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)); Đầu ra
2040-12-26