Skip to content

Commit 962d218

Browse files
committed
done till generic custom types
1 parent 57df163 commit 962d218

File tree

8 files changed

+316
-110
lines changed

8 files changed

+316
-110
lines changed

code/module-02/m02-start/module02.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,11 @@ if (ukCitizen) {
4040
it to have strongly typed variables. Then, address any errors you find so that the result
4141
returned to a is 12. */
4242

43-
enum num {
44-
five = 5,
45-
}
46-
let x: num;
43+
let x: number;
4744
let y: number;
4845
let a: number;
4946

50-
x = num.five;
47+
x = 5;
5148
y = 7;
5249
a = x + y;
5350

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
1-
// Empty
1+
"use strict";
2+
/* Module 3: Implement interfaces in TypeScript
3+
Lab Start */
4+
/* TODO: Update the calculateInterestOnlyLoanPayment function. */
5+
function calculateInterestOnlyLoanPayment(loanTerms) {
6+
// Calculates the monthly payment of an interest only loan
7+
let interest = loanTerms.interestRate / 1200; // Calculates the Monthly Interest Rate of the loan
8+
let payment;
9+
payment = loanTerms.principle * interest;
10+
return "The interest only loan payment is " + payment.toFixed(2);
11+
}
12+
/* TODO: Update the calculateConventionalLoanPayment function. */
13+
function calculateConventionalLoanPayment(loanTerms) {
14+
// Calculates the monthly payment of a conventional loan
15+
let interest = loanTerms.interestRate / 1200; // Calculates the Monthly Interest Rate of the loan
16+
let payment;
17+
payment =
18+
(loanTerms.principle * interest) /
19+
(1 - Math.pow(1 / (1 + interest), loanTerms.months));
20+
return "The conventional loan payment is " + payment.toFixed(2);
21+
}
22+
let interestOnlyPayment = calculateInterestOnlyLoanPayment({
23+
principle: 30000,
24+
interestRate: 5,
25+
});
26+
let conventionalPayment = calculateConventionalLoanPayment({
27+
principle: 30000,
28+
interestRate: 5,
29+
months: 180,
30+
});
31+
console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
32+
console.log(conventionalPayment); //* Returns "The conventional loan payment is 237.24"

code/module-03/m03-start/module03.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,45 @@
33

44
/* EXERCISE 1
55
TODO: Declare the Loan interface. */
6-
7-
6+
interface Loan {
7+
principle: number;
8+
interestRate: number;
9+
}
810
/* TODO: Declare the ConventionalLoan interface. */
9-
10-
11-
11+
interface ConventionalLoan extends Loan {
12+
months: number;
13+
}
1214
/* TODO: Update the calculateInterestOnlyLoanPayment function. */
1315

14-
function calculateInterestOnlyLoanPayment(principle, interestRate) {
15-
// Calculates the monthly payment of an interest only loan
16-
let interest = interestRate / 1200; // Calculates the Monthly Interest Rate of the loan
17-
let payment;
18-
payment = principle * interest;
19-
return 'The interest only loan payment is ' + payment.toFixed(2);
16+
function calculateInterestOnlyLoanPayment(loanTerms: Loan): string {
17+
// Calculates the monthly payment of an interest only loan
18+
let interest = loanTerms.interestRate / 1200; // Calculates the Monthly Interest Rate of the loan
19+
let payment;
20+
payment = loanTerms.principle * interest;
21+
return "The interest only loan payment is " + payment.toFixed(2);
2022
}
2123

2224
/* TODO: Update the calculateConventionalLoanPayment function. */
2325

24-
function calculateConventionalLoanPayment(principle, interestRate, months) {
25-
// Calculates the monthly payment of a conventional loan
26-
let interest = interestRate / 1200; // Calculates the Monthly Interest Rate of the loan
27-
let payment;
28-
payment = principle * interest / (1 - (Math.pow(1 / (1 + interest), months)));
29-
return 'The conventional loan payment is ' + payment.toFixed(2);
26+
function calculateConventionalLoanPayment(loanTerms: ConventionalLoan): string {
27+
// Calculates the monthly payment of a conventional loan
28+
let interest = loanTerms.interestRate / 1200; // Calculates the Monthly Interest Rate of the loan
29+
let payment;
30+
payment =
31+
(loanTerms.principle * interest) /
32+
(1 - Math.pow(1 / (1 + interest), loanTerms.months));
33+
return "The conventional loan payment is " + payment.toFixed(2);
3034
}
3135

32-
let interestOnlyPayment = calculateInterestOnlyLoanPayment(30000, 5);
33-
let conventionalPayment = calculateConventionalLoanPayment(30000, 5, 180);
34-
35-
console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
36-
console.log(conventionalPayment); //* Returns "The conventional loan payment is 237.24"
36+
let interestOnlyPayment = calculateInterestOnlyLoanPayment({
37+
principle: 30000,
38+
interestRate: 5,
39+
});
40+
let conventionalPayment = calculateConventionalLoanPayment({
41+
principle: 30000,
42+
interestRate: 5,
43+
months: 180,
44+
});
45+
46+
console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
47+
console.log(conventionalPayment); //* Returns "The conventional loan payment is 237.24"
Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,68 @@
1-
// Empty
1+
"use strict";
2+
/* Module 4: Develop typed functions using TypeScript
3+
Lab Start */
4+
/* TODO: Convert the sortDescending and sortAscending functions to arrow
5+
functions. */
6+
/* sortDescending is a comparison function that tells the sort method how to sort
7+
numbers in descending order */
8+
const sortDescending = (a, b) => {
9+
if (a > b) {
10+
return -1;
11+
}
12+
else if (b > a) {
13+
return 1;
14+
}
15+
else {
16+
return 0;
17+
}
18+
};
19+
/* sortDescending is a comparison function that tells the sort method how to sort
20+
numbers in ascending order. */
21+
const sortAscending = (a, b) => {
22+
if (a > b) {
23+
return 1;
24+
}
25+
else if (b > a) {
26+
return -1;
27+
}
28+
else {
29+
return 0;
30+
}
31+
};
32+
/* The buildArray function builds an array of unique random numbers containing the number
33+
of items based on the number passed to it. The sortOrder parameter determines
34+
whether to sort the array in ascending or descending order. */
35+
/* TODO: Update the BuildArray function. */
36+
function buildArray(items, sortOrder) {
37+
let randomNumbers = [];
38+
let nextNumber;
39+
for (let counter = 0; counter < items; counter++) {
40+
nextNumber = Math.ceil(Math.random() * (100 - 1));
41+
if (randomNumbers.indexOf(nextNumber) === -1) {
42+
randomNumbers.push(nextNumber);
43+
}
44+
else {
45+
counter--;
46+
}
47+
}
48+
if (sortOrder === "ascending") {
49+
return randomNumbers.sort(sortAscending);
50+
}
51+
else {
52+
return randomNumbers.sort(sortDescending);
53+
}
54+
}
55+
let myArray1 = buildArray(12, "ascending");
56+
let myArray2 = buildArray(8, "descending");
57+
console.log(myArray1);
58+
console.log(myArray2);
59+
/* EXERCISE 2
60+
TODO: Update the LoanCalculator function. */
61+
function loanCalculator(principle, interestRate, months = 12) {
62+
let interest = interestRate / 1200; // Calculates the monthly interest rate
63+
let payment;
64+
payment = (principle * interest) / (1 - Math.pow(1 / (1 + interest), months));
65+
return payment.toFixed(2);
66+
}
67+
let myLoan = loanCalculator(1000, 5);
68+
console.log(myLoan);

code/module-04/m04-start/module04.ts

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,81 @@
33

44
/* EXERCISE 1
55
TODO: Declare a new function type for the sortDescending and sortAscending functions. */
6-
7-
8-
/* TODO: Convert the sortDescending and sortAscending functions to arrow
6+
interface compareFunctionType {
7+
(a: number, b: number): number;
8+
}
9+
/* TODO: Convert the sortDescending and sortAscending functions to arrow
910
functions. */
1011

11-
/* sortDescending is a comparison function that tells the sort method how to sort
12+
/* sortDescending is a comparison function that tells the sort method how to sort
1213
numbers in descending order */
13-
function sortDescending(a, b) {
14-
if (a > b) {
14+
const sortDescending: compareFunctionType = (a, b) => {
15+
if (a > b) {
1516
return -1;
16-
} else if (b > a) {
17+
} else if (b > a) {
1718
return 1;
18-
} else {
19+
} else {
1920
return 0;
20-
}
21-
}
21+
}
22+
};
2223

23-
/* sortDescending is a comparison function that tells the sort method how to sort
24+
/* sortDescending is a comparison function that tells the sort method how to sort
2425
numbers in ascending order. */
25-
function sortAscending(a, b) {
26-
if (a > b) {
26+
const sortAscending: compareFunctionType = (a, b) => {
27+
if (a > b) {
2728
return 1;
28-
} else if (b > a) {
29+
} else if (b > a) {
2930
return -1;
30-
} else {
31+
} else {
3132
return 0;
32-
}
33-
}
33+
}
34+
};
3435

35-
/* The buildArray function builds an array of unique random numbers containing the number
36-
of items based on the number passed to it. The sortOrder parameter determines
36+
/* The buildArray function builds an array of unique random numbers containing the number
37+
of items based on the number passed to it. The sortOrder parameter determines
3738
whether to sort the array in ascending or descending order. */
3839

3940
/* TODO: Update the BuildArray function. */
4041

41-
function buildArray(items, sortOrder) {
42-
let randomNumbers = [];
43-
let nextNumber;
44-
for (let counter = 0; counter < items; counter++) {
45-
nextNumber = Math.ceil(Math.random() * (100 - 1));
46-
if (randomNumbers.indexOf(nextNumber) === -1) {
47-
randomNumbers.push(nextNumber);
48-
} else {
49-
counter--;
50-
}
51-
}
52-
if (sortOrder === 'ascending') {
53-
return randomNumbers.sort(sortAscending);
42+
function buildArray(
43+
items: number,
44+
sortOrder: "ascending" | "descending"
45+
): number[] {
46+
let randomNumbers: number[] = [];
47+
let nextNumber: number;
48+
for (let counter = 0; counter < items; counter++) {
49+
nextNumber = Math.ceil(Math.random() * (100 - 1));
50+
if (randomNumbers.indexOf(nextNumber) === -1) {
51+
randomNumbers.push(nextNumber);
5452
} else {
55-
return randomNumbers.sort(sortDescending);
53+
counter--;
5654
}
55+
}
56+
if (sortOrder === "ascending") {
57+
return randomNumbers.sort(sortAscending);
58+
} else {
59+
return randomNumbers.sort(sortDescending);
60+
}
5761
}
5862

59-
let myArray1 = buildArray(12, 'ascending');
60-
let myArray2 = buildArray(8, 'descending');
63+
let myArray1 = buildArray(12, "ascending");
64+
let myArray2 = buildArray(8, "descending");
65+
console.log(myArray1);
66+
console.log(myArray2);
6167

6268
/* EXERCISE 2
6369
TODO: Update the LoanCalculator function. */
6470

65-
function loanCalculator (principle, interestRate, months) {
66-
let interest = interestRate / 1200; // Calculates the monthly interest rate
67-
let payment;
68-
payment = principle * interest / (1 - (Math.pow(1/(1 + interest), months)));
69-
return payment.toFixed(2);
71+
function loanCalculator(
72+
principle: number,
73+
interestRate: number,
74+
months: number = 12
75+
) {
76+
let interest = interestRate / 1200; // Calculates the monthly interest rate
77+
let payment: number;
78+
payment = (principle * interest) / (1 - Math.pow(1 / (1 + interest), months));
79+
return payment.toFixed(2);
7080
}
81+
82+
let myLoan = loanCalculator(1000, 5);
83+
console.log(myLoan);
Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,74 @@
1-
// Empty
1+
"use strict";
2+
/* Module 5: Declare and instantiate classes in TypeScript
3+
Lab Start */
4+
/* EXERCISE 1 */
5+
class BuildArray {
6+
// TODO Define the constructor
7+
constructor(items, sortOrder) {
8+
// TODO Define the methods.
9+
this.sortDescending = (a, b) => {
10+
if (a > b) {
11+
return -1;
12+
}
13+
else if (b > a) {
14+
return 1;
15+
}
16+
else {
17+
return 0;
18+
}
19+
};
20+
this.sortAscending = (a, b) => {
21+
if (a > b) {
22+
return 1;
23+
}
24+
else if (b > a) {
25+
return -1;
26+
}
27+
else {
28+
return 0;
29+
}
30+
};
31+
this._items = items;
32+
this._sortOrder = sortOrder;
33+
}
34+
// TODO Define the accessors
35+
get items() {
36+
return this._items;
37+
}
38+
set items(items) {
39+
this._items = items;
40+
}
41+
get sortOrder() {
42+
return this._sortOrder;
43+
}
44+
set sortOrder(sortOrder) {
45+
this._sortOrder = sortOrder;
46+
}
47+
/* buildArray builds an array of unique random numbers containing the number of items
48+
based on the number passed to it. The sortOrder parameter determines whether to sort
49+
the array in ascending or descending order. */
50+
buildArray() {
51+
let randomNumbers = [];
52+
let nextNumber;
53+
for (let counter = 0; counter < this.items; counter++) {
54+
nextNumber = Math.ceil(Math.random() * (100 - 1));
55+
if (randomNumbers.indexOf(nextNumber) === -1) {
56+
randomNumbers.push(nextNumber);
57+
}
58+
else {
59+
counter--;
60+
}
61+
}
62+
if (this._sortOrder === "ascending") {
63+
return randomNumbers.sort(this.sortAscending);
64+
}
65+
else {
66+
return randomNumbers.sort(this.sortDescending);
67+
}
68+
}
69+
}
70+
/* TODO: Instantiate the BuildArray objects. */
71+
let testArray1 = new BuildArray(12, "ascending");
72+
let testArray2 = new BuildArray(8, "descending");
73+
console.log(testArray1);
74+
console.log(testArray2);

0 commit comments

Comments
 (0)