I am trying to build an EMI calculator using velo on one of the pages on my website. I added a container with 3 input fields for the user to enter loan amount, tenure and rate of interest and a calculate button at the bottom, on click of the button, system should calculate the EMI and display it on screen. All these 3 figures are correctly being entered into the dataset as well, but the calculate button is not working. Additionally, i want to display a pie chart as well showing the principal and interest component from the total EMI and an ammortization schedule with a download option.
Below is the code that i have used. Any suggestions and improvements will be appreciated.
import wixData from ‘wix-data’ ;
import wixDataset from ‘wix-dataset’ ;
import wixbak
// get references to the input fields and submit button
const loanAmount = $w ( “#loanAmount” );
const rateOfInterest = $w ( “#rateOfInterest” );
const tenureYears = $w ( “#tenureYears” );
const submitButton = $w ( “#submit” );
// add event listener to the submit button
submitButton . onClick (() => {
// get the input values
const amount = Number ( loanAmount . value );
const rate = Number ( rateOfInterest . value );
const tenure = Number ( tenureYears . value );
// calculate the EMI
const monthlyInterest = rate / ( 12 * 100 );
const termInMonths = tenure * 12 ;
const emi = ( amount * ( monthlyInterest ) * ( Math . pow (( 1 + ( monthlyInterest )), termInMonths ))) / (( Math . pow (( 1 + monthlyInterest ), termInMonths )) - 1 );
console . log ( “EMI:” , emi ); // log the EMI value to the console
// save the EMI to the dataset
const newEmi = {
“amount” : amount ,
“rate” : rate ,
“tenure” : tenure ,
“emi” : emi . toFixed ( 2 )
};
wixData . insert ( “dataset1” , newEmi )
. then (() => {
// display the EMI on the page
$w ( “#emiAmount” ). text = emi . toFixed ( 2 );
})
. catch (( error ) => {
console . log ( error );
});
});