Challenge: Abstract Pattern

In this challenge, you have to implement the abstract pattern to solve the given problem.

Problem statement

In this challenge, you have the abstract constructor function Loans. There are three types of loans that can be given:

  • HomeLoan

  • StudentLoan

  • PersonalLoan

You need to implement Loans such that it decides which loan instance to instantiate, given the input parameters. Implement a function getloan for this purpose. getloan should take the following parameters:

  • type: type of loan

  • amount: amount of loan

  • duration: duration of loan

All three loans mentioned above have the following properties:

  • amount: the amount of loan required (in dollars)

  • duration: the duration of time (in years) for which the loan is borrowed

  • interest: the annual interest rate

The interest is different but fixed for all three loans:

  • The interest on HomeLoan is: 0.08

  • The interest on StudentLoan is: 0.03

  • The interest on PersonalLoan is: 0.05

Also defined for these loans is the calculateInterest method that calculates the simple interest on the amount borrowed for the given duration. Use the following formula to calculate the total interest:

TotalInterest=amountinterestdurationTotal Interest = amount * interest * duration

Input

calculateInterest method called for the loan chosen

Output

The amount of total interest on the money borrowed

Sample input

var loan = new Loans()

var homeLoan = loan.getloan('home',3200,5)
homeLoan.calculateInterest()

var studentLoan = loan.getloan('student',1800,4)
studentLoan.calculateInterest()

var personalLoan = loan.getloan('personal',1200,2)
personalLoan.calculateInterest()

Sample output

1280
216
120

Challenge #

Take a close look and design a step-by-step solution before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided. Good Luck!

//implement other functions here
function Loans(){
//implement Loan function here
}

Let’s discuss the solution in the next lesson.