Skip to content

Commit 0754c1d

Browse files
committed
feat: namespace exercise MicrosoftDocs#1 done
1 parent ca9ed0a commit 0754c1d

File tree

2 files changed

+75
-26
lines changed

2 files changed

+75
-26
lines changed
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
1-
// Empty
1+
"use strict";
2+
/* Module 8: Organize code using TypeScript namespaces
3+
Lab Start */
4+
/* TODO Create LoanPrograms namespace. */
5+
var LoanPrograms;
6+
(function (LoanPrograms) {
7+
/* TODO Update the calculateInterestOnlyLoanPayment function. */
8+
function calculateInterestOnlyLoanPayment(loanTerms) {
9+
let payment;
10+
payment = loanTerms.principle * calculateInterestRate(loanTerms.interestRate);
11+
return "The interest only loan payment is " + payment.toFixed(2);
12+
}
13+
LoanPrograms.calculateInterestOnlyLoanPayment = calculateInterestOnlyLoanPayment;
14+
/* TODO Update the calculateConventionalLoanPayment function. */
15+
function calculateConventionalLoanPayment(loanTerms) {
16+
let interest = calculateInterestRate(loanTerms.interestRate);
17+
let payment;
18+
payment =
19+
(loanTerms.principle * interest) /
20+
(1 - Math.pow(1 / (1 + interest), loanTerms.months));
21+
return "The conventional loan payment is " + payment.toFixed(2);
22+
}
23+
LoanPrograms.calculateConventionalLoanPayment = calculateConventionalLoanPayment;
24+
function calculateInterestRate(interestRate) {
25+
let interest = interestRate / 1200;
26+
return interest;
27+
}
28+
})(LoanPrograms || (LoanPrograms = {}));
29+
/* TODO Add reference paths. */
30+
/* TODO Update the function calls. */
31+
let interestOnlyPayment = LoanPrograms.calculateInterestOnlyLoanPayment({
32+
principle: 30000,
33+
interestRate: 5,
34+
});
35+
let conventionalLoanPayment = LoanPrograms.calculateConventionalLoanPayment({
36+
principle: 30000,
37+
interestRate: 5,
38+
months: 180,
39+
});
40+
console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
41+
console.log(conventionalLoanPayment); //* Returns "The conventional loan payment is 237.24"

code/module-08/m08-start/module08_main.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,51 @@
22
Lab Start */
33

44
/* TODO Create a the Loans namespace. */
5-
6-
7-
interface Loan {
8-
principle: number,
9-
interestRate: number //* Interest rate percentage (eg. 14 is 14%)
10-
}
11-
interface ConventionalLoan extends Loan {
12-
months: number //* Total number of months
5+
namespace Loans {
6+
export interface Loan {
7+
principle: number;
8+
interestRate: number; //* Interest rate percentage (eg. 14 is 14%)
9+
}
10+
export interface ConventionalLoan extends Loan {
11+
months: number; //* Total number of months
12+
}
1313
}
1414

1515
/* TODO Create LoanPrograms namespace. */
16-
17-
18-
/* TODO Update the calculateInterestOnlyLoanPayment function. */
19-
function calculateInterestOnlyLoanPayment(loanTerms: Loan): string {
16+
namespace LoanPrograms {
17+
/* TODO Update the calculateInterestOnlyLoanPayment function. */
18+
export function calculateInterestOnlyLoanPayment(loanTerms: Loans.Loan): string {
2019
let payment: number;
2120
payment = loanTerms.principle * calculateInterestRate(loanTerms.interestRate);
22-
return 'The interest only loan payment is ' + payment.toFixed(2);
23-
}
24-
/* TODO Update the calculateConventionalLoanPayment function. */
25-
function calculateConventionalLoanPayment(loanTerms: ConventionalLoan): string {
21+
return "The interest only loan payment is " + payment.toFixed(2);
22+
}
23+
/* TODO Update the calculateConventionalLoanPayment function. */
24+
export function calculateConventionalLoanPayment(loanTerms: Loans.ConventionalLoan): string {
2625
let interest: number = calculateInterestRate(loanTerms.interestRate);
2726
let payment: number;
28-
payment = loanTerms.principle * interest / (1 - (Math.pow(1/(1 + interest), loanTerms.months)));
29-
return 'The conventional loan payment is ' + payment.toFixed(2);
30-
}
31-
function calculateInterestRate (interestRate: number): number {
27+
payment =
28+
(loanTerms.principle * interest) /
29+
(1 - Math.pow(1 / (1 + interest), loanTerms.months));
30+
return "The conventional loan payment is " + payment.toFixed(2);
31+
}
32+
function calculateInterestRate(interestRate: number): number {
3233
let interest: number = interestRate / 1200;
33-
return interest
34+
return interest;
35+
}
3436
}
3537

3638
/* TODO Add reference paths. */
3739

3840
/* TODO Update the function calls. */
3941

40-
let interestOnlyPayment = calculateInterestOnlyLoanPayment({principle: 30000, interestRate: 5});
41-
let conventionalLoanPayment = calculateConventionalLoanPayment({principle: 30000, interestRate: 5, months: 180});
42-
console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
43-
console.log(conventionalLoanPayment); //* Returns "The conventional loan payment is 237.24"
42+
let interestOnlyPayment = LoanPrograms.calculateInterestOnlyLoanPayment({
43+
principle: 30000,
44+
interestRate: 5,
45+
});
46+
let conventionalLoanPayment = LoanPrograms.calculateConventionalLoanPayment({
47+
principle: 30000,
48+
interestRate: 5,
49+
months: 180,
50+
});
51+
console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
52+
console.log(conventionalLoanPayment); //* Returns "The conventional loan payment is 237.24"

0 commit comments

Comments
 (0)