Skip to content

Commit ca9ed0a

Browse files
committed
till module08
1 parent 962d218 commit ca9ed0a

File tree

9 files changed

+130
-33
lines changed

9 files changed

+130
-33
lines changed
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
1-
// Empty
1+
"use strict";
2+
/* Module 6: DGenerics in TypeScript
3+
Lab Start */
4+
/* DataStore is a utility function that can store up to 10 string values in an array.
5+
Rewrite the DataStore class so the array can store items of any type.
6+
7+
TODO: Add and apply a type variable. */
8+
class DataStore {
9+
constructor() {
10+
this._data = new Array(10);
11+
}
12+
AddOrUpdate(index, item) {
13+
if (index >= 0 && index < 10) {
14+
this._data[index] = item;
15+
}
16+
else {
17+
console.log("Index is greater than 10");
18+
}
19+
}
20+
GetData(index) {
21+
if (index >= 0 && index < 10) {
22+
return this._data[index];
23+
}
24+
else {
25+
return;
26+
}
27+
}
28+
}
29+
let cities = new DataStore();
30+
cities.AddOrUpdate(0, "Mumbai");
31+
cities.AddOrUpdate(1, "Chicago");
32+
cities.AddOrUpdate(11, "London"); // item not added
33+
console.log(cities.GetData(1)); // returns 'Chicago'
34+
console.log(cities.GetData(12)); // returns 'undefined'
35+
// TODO Test items as numbers.
36+
let empIDs = new DataStore();
37+
empIDs.AddOrUpdate(0, 50);
38+
empIDs.AddOrUpdate(1, 65);
39+
empIDs.AddOrUpdate(2, 89);
40+
console.log(empIDs.GetData(0)); // returns 50
41+
let pets = new DataStore();
42+
pets.AddOrUpdate(0, { name: 'Rex', breed: 'Golden Retriever', age: 5 });
43+
pets.AddOrUpdate(1, { name: 'Sparky', breed: 'Jack Russell Terrier', age: 3 });
44+
console.log(pets.GetData(1)); // returns { name: 'Sparky', breed: 'Jack Russell Terrier', age: 3 }
Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
11
/* Module 6: DGenerics in TypeScript
22
Lab Start */
33

4-
/* DataStore is a utility function that can store up to 10 string values in an array.
4+
/* DataStore is a utility function that can store up to 10 string values in an array.
55
Rewrite the DataStore class so the array can store items of any type.
66
77
TODO: Add and apply a type variable. */
8-
class DataStore {
9-
10-
private _data = new Array(10);
11-
12-
AddOrUpdate(index: number, item: string) {
13-
if(index >=0 && index <10) {
14-
this._data[index] = item;
15-
} else {
16-
alert('Index is greater than 10')
17-
}
8+
class DataStore<T> {
9+
private _data: Array<T> = new Array(10);
10+
11+
AddOrUpdate(index: number, item: T) {
12+
if (index >= 0 && index < 10) {
13+
this._data[index] = item;
14+
} else {
15+
console.log("Index is greater than 10");
1816
}
19-
GetData(index: number) {
20-
if(index >=0 && index < 10) {
21-
return this._data[index];
22-
} else {
23-
return
24-
}
17+
}
18+
GetData(index: number) {
19+
if (index >= 0 && index < 10) {
20+
return this._data[index];
21+
} else {
22+
return;
2523
}
24+
}
2625
}
2726

2827
let cities = new DataStore();
2928

3029
cities.AddOrUpdate(0, "Mumbai");
3130
cities.AddOrUpdate(1, "Chicago");
32-
cities.AddOrUpdate(11, "London"); // item not added
31+
cities.AddOrUpdate(11, "London"); // item not added
3332

34-
console.log(cities.GetData(1)); // returns 'Chicago'
35-
console.log(cities.GetData(12)); // returns 'undefined'
33+
console.log(cities.GetData(1)); // returns 'Chicago'
34+
console.log(cities.GetData(12)); // returns 'undefined'
3635

3736
// TODO Test items as numbers.
38-
39-
37+
let empIDs = new DataStore<number>();
38+
empIDs.AddOrUpdate(0, 50);
39+
empIDs.AddOrUpdate(1, 65);
40+
empIDs.AddOrUpdate(2, 89);
41+
console.log(empIDs.GetData(0)); // returns 50
4042
// TODO Test items as objects.
43+
type Pets = {
44+
name: string;
45+
breed: string;
46+
age: number
47+
}
48+
let pets = new DataStore<Pets>();
49+
pets.AddOrUpdate(0, { name: 'Rex', breed: 'Golden Retriever', age: 5});
50+
pets.AddOrUpdate(1, { name: 'Sparky', breed: 'Jack Russell Terrier', age: 3});
51+
console.log(pets.GetData(1)); // returns { name: 'Sparky', breed: 'Jack Russell Terrier', age: 3 }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.calculateConventionalLoanPayment = exports.calculateInterestOnlyLoanPayment = void 0;
4+
/* TODO Update the calculateInterestOnlyLoanPayment function. */
5+
function calculateInterestOnlyLoanPayment(loanTerms) {
6+
var payment;
7+
payment = loanTerms.principle * calculateInterestRate(loanTerms.interestRate);
8+
return 'The interest only loan payment is ' + payment.toFixed(2);
9+
}
10+
exports.calculateInterestOnlyLoanPayment = calculateInterestOnlyLoanPayment;
11+
/* TODO Update the calculateConventionalLoanPayment function. */
12+
function calculateConventionalLoanPayment(loanTerms) {
13+
var interest = calculateInterestRate(loanTerms.interestRate);
14+
var payment;
15+
payment = loanTerms.principle * interest / (1 - (Math.pow(1 / (1 + interest), loanTerms.months)));
16+
return 'The conventional loan payment is ' + payment.toFixed(2);
17+
}
18+
exports.calculateConventionalLoanPayment = calculateConventionalLoanPayment;
19+
function calculateInterestRate(interestRate) {
20+
var interest = interestRate / 1200;
21+
return interest;
22+
}

code/module-07/m07-start/module07_loan-programs.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/* TODO Add the import statement. */
2+
import * as Loans from './module07_loans.js';
23

34
/* TODO Update the calculateInterestOnlyLoanPayment function. */
4-
function calculateInterestOnlyLoanPayment(loanTerms: Loan): string {
5+
export function calculateInterestOnlyLoanPayment(loanTerms: Loans.Loan): string {
56
let payment: number;
67
payment = loanTerms.principle * calculateInterestRate(loanTerms.interestRate);
78
return 'The interest only loan payment is ' + payment.toFixed(2);
89
}
910
/* TODO Update the calculateConventionalLoanPayment function. */
10-
function calculateConventionalLoanPayment(loanTerms: ConventionalLoan): string {
11+
export function calculateConventionalLoanPayment(loanTerms: Loans.ConventionalLoan): string {
1112
let interest: number = calculateInterestRate(loanTerms.interestRate);
1213
let payment: number;
1314
payment = loanTerms.principle * interest / (1 - (Math.pow(1/(1 + interest), loanTerms.months)));
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* TODO Update the interface declarations. */
2-
interface Loan {
2+
export interface Loan {
33
principle: number,
44
interestRate: number //* Interest rate percentage (eg. 14 is 14%)
55
}
6-
interface ConventionalLoan extends Loan {
6+
export interface ConventionalLoan extends Loan {
77
months: number //* Total number of months
88
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
/* Module 7: Working with external libraries
3+
Lab Start */
4+
Object.defineProperty(exports, "__esModule", { value: true });
5+
/* TODO Add the import statement. */
6+
var LoanPrograms = require("./module07_loan-programs.js");
7+
/* TODO Update the function calls. */
8+
var interestOnlyPayment = LoanPrograms.calculateInterestOnlyLoanPayment({ principle: 30000, interestRate: 5 });
9+
var conventionalLoanPayment = LoanPrograms.calculateConventionalLoanPayment({ principle: 30000, interestRate: 5, months: 180 });
10+
console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
11+
console.log(conventionalLoanPayment); //* Returns "The conventional loan payment is 237.24"

code/module-07/m07-start/module07_main.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
Lab Start */
33

44
/* TODO Add the import statement. */
5-
5+
import * as LoanPrograms from "./module07_loan-programs.js";
66
/* TODO Update the function calls. */
7-
let interestOnlyPayment = calculateInterestOnlyLoanPayment({principle: 30000, interestRate: 5});
8-
let conventionalLoanPayment = calculateConventionalLoanPayment({principle: 30000, interestRate: 5, months: 180});
9-
console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
10-
console.log(conventionalLoanPayment); //* Returns "The conventional loan payment is 237.24"
7+
let interestOnlyPayment = LoanPrograms.calculateInterestOnlyLoanPayment({
8+
principle: 30000,
9+
interestRate: 5,
10+
});
11+
let conventionalLoanPayment = LoanPrograms.calculateConventionalLoanPayment({
12+
principle: 30000,
13+
interestRate: 5,
14+
months: 180,
15+
});
16+
console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
17+
console.log(conventionalLoanPayment); //* Returns "The conventional loan payment is 237.24"

code/module-07/m07-start/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"compilerOptions": {
33
/* Visit https://aka.ms/tsconfig.json to read more about this file */
44

0 commit comments

Comments
 (0)