import React, { useEffect, useState } from "react"; import FormInputGroup from "./FormInputGroup"; const LoanCalculator = () => { const [loanAmount, setLoanAmount] = useState(0); const [interestRate, setInterestRate] = useState(""); const [loanDuration, setLoanDuration] = useState(""); const [monthlyPayment, setMonthlyPayment] = useState(0); const [interestPaid, setInterestPaid] = useState(0); const calculateMonthlyPayment = () => { const percentageToDecimal = (percent) => { return percent / 12 / 100; } const yearsToMonths = (years) => { return years * 12; } setMonthlyPayment(percentageToDecimal(interestRate * loanAmount) / (1 - Math.pow(1 + percentageToDecimal(interestRate), -yearsToMonths(loanDuration)))); } useEffect(() => { setInterestPaid(monthlyPayment * (loanDuration * 12) - loanAmount); }, [monthlyPayment]); return ( <>