Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JavaScript (No PHP) Metro Bank Mortgage Calculator You\'ve been hired to do some

ID: 3725433 • Letter: J

Question

JavaScript (No PHP)

Metro Bank Mortgage Calculator

You've been hired to do some programming for Metro Bank. They want you to create a program to calculate loan payments. The user will enter the amount of money to borrow from the bank, the annual interest rate percentage (that will be compounded monthly) and the number of years to pay back the loan. The program will then display the monthly payment and the total amount of interest that will be paid.

Formulas & Code Specifications

Here are the formulas you will use.

Periodic Payment:
Let

A = amount of the loan
r = interest rate per period (for this calculation, 1 period is 1 month)
n = the number of periods (i.e. the number of months)

Then the formula to find the monthly payment is:

Remember, when doing calculations with percentages, you need to use the decimal equivalent. So given a percentage rate of 7.5% you want to use the value 0.075.

Total Interest Paid:

Let

P = the monthly payment
n = the number of months
A = amount of the loan

Then the formula to find the total interest is:

Total Interest = nP - A

------------------

This is the php that I started with, that can be used to convert to javascript...no php

--------LoanCal.php----------

<!DOCTYPE html>
<!--
* Description: This is loan (mortgage) calculator accepts user input to
* calculate their loan re-payment time and amount (based on loan
* amount and compounding interest.)
-->

<html>
<head>
<title>Product Discount Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>

<body>
<h1 align="center"> Loan Calculator </h1>
<div>
<form>
<table align="center">
<tr>
<td> Loan Amount</td>
<td><input name="loan_amount" type="number" step="0.05" value="" required/></td>
</tr>
<tr>
<td> Interest Rate (annual %)</td>
<td><input name="annual_rate" type="number" value=""/></td>
</tr>
<tr>
<td> Period in years</td>
<td><input name="years" type="number" value=""/></td>
</tr>
<tr>

<td colspan="4">
<input type="submit" align="center"> </td>
</tr>

</table>
</form>

</div>
<?php
//Loan amount
$loanAmount = $_REQUEST['loan_amount'];
//Monthly rate
$annualRate = $_REQUEST['annual_rate'];
//Months in the loan period
$periodInYears = $_REQUEST['years'];


if ($loanAmount && $annualRate && $periodInYears) { // Check if these values are valid.
$monthRate = $annualRate / 12 / 100;
//Start to do your work here. Don't use operator '**'.
  
//Convert years to months(periods)
$numberOfPeriods = $periodInYears * 12;
  
//Calculating numerator of the formula i.e A*r
$numerator = $loanAmount*$monthRate;
  
//Calculating denominator of the formula i.e (1 - pow((1+r),(-n)))
$denominator = (1 - pow((1 + $monthRate), -($numberOfPeriods)));
  
//Calculating Monthly Payment i.e numerator/denominator
$payment = round($numerator/$denominator, 2);
  
//Calculating Total Interest i.e nP-A
$totalInterest = $numberOfPeriods * $payment - $loanAmount;
?>

<h1 align="center">Your Loan Details</h1>
  
<div>
<form>
<table align="center">
<tr><td>Loan Amount: </td>
<td><?php echo "$$loanAmount<br/>";?></td>
</tr>
  
<tr><td>Annual Rate: </td>
<td><?php echo "$annualRate%<br/>";?></td>
</tr>
  
<tr><td>Loan Period: </td>
<td><?php echo "$periodInYears years<br/>";?></td>
</tr>
  
<tr><td>Monthly Payment: </td>
<td><?php echo "$$payment <br/>";?></td>
</tr>
  
<tr>
<td>Total Interest: </td>
<td><?php echo "$$totalInterest <br/>";?></td>
</tr>

</table>
</form>

</div>
<?php
  
}
?>
</body>

</html>

Explanation / Answer

SOurceCode:

<?php
namespace CentralAppsMortgageCalculator;
/**
* PHP Mortgage Calculator Class
* @author Michael Peacock
* @url www.michaelpeacock.co.uk
*/
class Calculator {
/**
* The amount borrowed
* @access private
*/
private $amountBorrowed = 0;
/**
* The interest rate
* @access private
*/
private $interestRate = 0;
/**
* Number of years to spread mortgage across
* @access private
*/
private $years = 0;
public function __construct() {}
/**
* Calculates the monthly cost of an interest only mortgage
* @return int
*/
public function calculateInterestOnlyPayment()
{
$payment = ( $this->amountBorrowed * $this->interestRate ) / 12;
return round($payment,2);
}
/**
* Calculates the monthly cost of a repayment mortgage
* @return int
*/
public function calculateRepayment()
{
$payment = ( $this->amountBorrowed * $this->interestRate ) / ( 1 - pow( ( 1 + $this->interestRate ), -$this->years ) ) / 12;
return round($payment,2);
}
/**
* Sets the amount borrowed
* @param int $amount the amount borrowed
* @return void
*/
public function setAmountBorrowed( $amount )
{
$this->amountBorrowed = $amount;
}
/**
* Sets the interest rate to calculate against
* @param int $rate the interest rate
* @return void
*/
public function setInterestRate( $rate )
{
$this->interestRate = ( $rate / 100 );
}
/**
* Set the number of years to spread the mortgage across
* @param int $years the number of years
* @return void
*/
public function setYears( $years )
{
$this->years = $years;
}
/**
* Calculate the number of monthly payments
* @return int
*/
private function numberOfPayments()
{
return $this->years / 12;
}
/**
* Calculate the amount of mortgage remaining based off how long remains
* @param int $monthsLeft the number of months left on the mortgage
* @return int
*/
private function calculateRemainingAmount( $monthsLeft )
{
return $this->calculateRepayment() * $monthsLeft;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote