Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Tìm ngày tiền gửi đạt số tiền cụ thể trong JavaScript

Vấn đề

Chúng ta có một số tiền amt > 0 và gửi nó với lãi suất p phần trăm chia 360 mỗi ngày vào ngày 1 tháng 1 năm 2021. Chúng ta muốn có số tiền total >= a0. Hàm 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ụ

Dưới đây là mã nguồn:

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));

Kết quả

2040-12-26