# Building an EMI Calculator using JavaScript : 3 different methods

I have recently started teaching myself JavaScript. As a first practical assignment I decided to build an EMI calculator purely using JavaScript. At this stage I am just exploring various ways to implement the logic (without worrying about UI). As a next step I intend to build a ready to use calculator tool & publish it on the site.

Here are the 3 methods that I tried:

• Simple implementation using operators
• Implementation using Functions
• Implementation using Functions & Objects

## Simple Implementation using operators

This was simple. The formula for calculating EMI is available all over the web. Here’s what it is :

EMI = (P*R* (1+R/100)^T) /( (1+R/100)^T -1)

P = Loan Amount

R = Rate of Interest (monthly)

T= Number of Months required for repayment

To be fair JavaScript logic & syntax is pretty simple & straightforward. Though Python will always remain my favorite(in terms of simplicity).

Here’s how the code looks like:

``````// Declaring & Assigning variables
let loanAmount = 10000000;
let annualInterestrate = 15; // Annual Percentage Rate (to be converted monthly)
let loanDuration = 20;// In years. To be converted to Months.

//EMI Calculation
let interest = annualInterestrate/1200;
let term = loanDuration*12;
let top = Math.pow((1+interest),term);
let bottom = top - 1;
let ratio = top/bottom;
EMI = loanAmount * interest * ratio; // Monthly Repayment
Total = EMI*term // Total Repayment

//Checking Output
console.log(EMI,Total)``````

## Implementation using functions

I then went on to implement the same logic using functions :

``````//EMI Calculation Function
function EMIVal(loanAmount,annualInterestrate,loanDuration){
interest = annualInterestrate/1200;
let term = loanDuration*12;

let top = Math.pow((1+interest),term);
let bottom = top - 1;
let ratio = top/bottom;

EMI = loanAmount * interest * ratio;
Total = EMI*term

return EMI

}
//Checking Output on console
console.log(EMIVal(10000000,15,20).toFixed(0))``````

Note how I am passing the values at the time of checking output. The limitation in the above code however is the fact that the function is just returning the EMI value & not the total value. I will tackle the issue in the next approach.

## Implementation using functions & Objects

I wanted to try out objects primarily because I wanted the function to return both EMI & Total as output. However, I then thought of the possibility of clubbing all the arguments to the function in a single object. The approach definitely was cleaner & most scalable:

``````// Definitng the loan object
const loanstruct = {
loanAmount:10000000,
annualInterestrate:15,
loanDuration:20
};
// Passing the object as the arguement. The function also returns an object that includes both EMI & Total
function EMIVal2(loan){
interest = loan.annualInterestrate/1200;
let term = loan.loanDuration*12;
let top = Math.pow((1+interest),term);
let bottom = top - 1;
let ratio = top/bottom;
EMI = loan.loanAmount * interest * ratio;
Total = EMI*term;
const EMIObj = {
EMI:EMI.toFixed(0),
Total:Total.toFixed(0)
};
return EMIObj;
}
// Checking the output
console.log(EMIVal2(loanstruct))``````

## Next Steps

I am now looking forward to integrating the above logic with some HTML & CSS to create a EMI calculator that anyone would be able to use.

## 1 thought on “Building an EMI Calculator using JavaScript : 3 different methods”

1. cbd says: