Challenge: Solution Review
This lesson will explain the solution to the problem from the last coding challenge.
We'll cover the following
Solution #
function HomeLoan(amount,duration){this.amount = amountthis.interest = 0.08this.duration = durationthis.calculateInterest = function(){return this.amount*this.interest*this.duration}}function StudentLoan(amount,duration){this.amount = amountthis.interest = 0.03this.duration = durationthis.calculateInterest = function(){return this.amount*this.interest*this.duration}}function PersonalLoan(amount,duration){this.amount = amountthis.interest = 0.05this.duration = durationthis.calculateInterest = function(){return this.amount*this.interest*this.duration}}function Loans(){this.getloan = function(type,amount,duration){var loan;switch(type){case 'home':loan = new HomeLoan(amount,duration)break;case 'student':loan = new StudentLoan(amount,duration)break;case 'personal':loan = new PersonalLoan(amount,duration)break;default :loan = nullbreak;}return loan}}var loan = new Loans()var homeLoan = loan.getloan('home',3200,5)console.log(homeLoan.calculateInterest())var studentLoan = loan.getloan('student',1800,4)console.log(studentLoan.calculateInterest())var personalLoan = loan.getloan('personal',1200,2)console.log(personalLoan.calculateInterest())
Explanation #
To make the problem easier to understand, let’s start by converting it into a diagram.
If you follow the diagram and the code, you’ll see that there is an abstract Loans
constructor function. Its purpose is to instantiate an instance of a specific loan constructor depending on the parameters given.
From the diagram above, you can see that three types of loan objects can be instantiated:
-
PersonalLoan
-
StudentLoan
-
HomeLoan
These loans, in turn, have different types available. In our coding challenge, we do not go into these sub-categories; however, knowing this will help you in visualizing the overall picture.
Coming back to our code. Let’s start by understanding how Loans
is following an abstract pattern.
-
Inside the
Loan
function, we first define thegetloan
method.function Loans(){ this.getloan = function(type,amount,duration){ //code } }
As you can see, it accepts the parameters
type
,amount
, andduration
. -
Next, it defines the variable
loan
, which will store an instance of a specific loan at a given time.var loan;
-
Depending on the
type
of loan required, thegetloan
function instantiates an instance of that loan. For example, iftype
equalshome
it instantiatesHomeLoan
instance.loan = new HomeLoan(amount,duration)
The same goes for the
student
andpersonal
type of loans. -
In the end, the
getloan
function returns the specificloan
instance.
Now let’s look at how the instance for a specific type of loan is created. As we discussed, we have three types, and the constructor for each is declared as follows:
function HomeLoan(amount,duration){
//code..
}
function StudentLoan(amount,duration){
//code..
}
function PersonalLoan(amount,duration){
//code..
}
As mentioned in the question, all three contain the properties amount
, interest
, and duration
. These properties are initialized in the body of the constructor.
function HomeLoan(amount,duration){
this.amount = amount
this.interest = 0.08
this.duration = duration
}
function StudentLoan(amount,duration){
this.amount = amount
this.interest = 0.03
this.duration = duration
}
function PersonalLoan(amount,duration){
this.amount = amount
this.interest = 0.05
this.duration = duration
}
Since the interest
parameter is fixed for each loan, we set its value to the specified number in the constructor.
Lastly, we define the calculateInterest
method in the constructor of each loan.
this.calculateInterest = function(){
return this.amount*this.interest*this.duration
}
Using the formula provided in the question, it multiplies the amount
, the interest
, and the duration
of the current loan
instance to calculate the final interest amount.
Now that the abstract pattern has been implemented, all you have to do to create an instance of a loan.
var loan = new Loans()
var homeLoan = loan.getloan('home',3200,5)
After creating an instance of Loans
. Invoke the getloan
method on it with the parameter that specifies the type of loan you want. This method will take care of the rest and create the required loan. You won’t need to use the new
operator to create different types of loans yourself.
Now that you have learned about the creational patterns let’s discuss structural patterns in the next lesson.