Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions code/module-04/m04-end/build/module04.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
"use strict";
/* Module 4: Develop typed functions using TypeScript
Lab End */
/* Module 4: Develop typed functions using TypeScript
Lab End */
/* TODO: Convert the sortDescending and sortAscending functions to anonymous
functions and assign them to variables of the same name. Add types to the
parameter lists and return values for each function. */
/* sortDescending is a comparison function that tells the sort method how to sort
functions. */
/* sortDescending is a comparison function that tells the sort method how to sort
numbers in descending order */
let sortDescending = (a, b) => {
if (a > b) {
return -1;
;
}
else if (b > a) {
return 1;
;
}
else {
return 0;
Expand All @@ -35,18 +32,17 @@ let sortAscending = (a, b) => {
/* The buildArray function builds an array of unique random numbers containing the number
of items based on the number passed to it. The sortOrder parameter determines
whether to sort the array in ascending or descending order. */
/* TODO: In the BuildArray function, add types to parameter list,
return value, and variables. */
/* TODO: Update the BuildArray function. */
function buildArray(items, sortOrder) {
let randomNumbers = [];
let nextNumber;
for (let i = 0; i < items; i++) {
nextNumber = Math.floor(Math.random() * (100 - 1)) + 1;
for (let counter = 0; counter < items; counter++) {
nextNumber = Math.ceil(Math.random() * (100 - 1));
if (randomNumbers.indexOf(nextNumber) === -1) {
randomNumbers.push(nextNumber);
}
else {
i--;
counter--;
}
}
if (sortOrder === 'ascending') {
Expand All @@ -61,15 +57,12 @@ let myArray2 = buildArray(8, 'descending');
console.log(myArray1);
console.log(myArray2);
/* EXERCISE 2
TODO: Convert the LoanCalc function to TypeScript with strongly typed parameters, variables,
and return types. Make the numMonths parameter optional but assign it a default value of 12
months if omitted. */
function LoanCalc(principle, interestRate, numMonths = 12) {
let intr = interestRate / 1200; // Calculates the monthly interest rate
let pmt;
Number;
pmt = principle * intr / (1 - (Math.pow(1 / (1 + intr), numMonths)));
return pmt.toFixed(2);
TODO: Update the LoanCalculator function. */
function loanCalculator(principle, interestRate, months = 12) {
let interest = interestRate / 1200; // Calculates the monthly interest rate
let payment;
payment = principle * interest / (1 - (Math.pow(1 / (1 + interest), months)));
return payment.toFixed(2);
}
let myLoan = LoanCalc(1000, 5);
let myLoan = loanCalculator(1000, 5);
console.log(myLoan);
6 changes: 3 additions & 3 deletions code/module-04/m04-end/module04.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
numbers in descending order */
let sortDescending: compareFunctionType = (a, b) => {
if (a > b) {
return -1;;
return -1;
} else if (b > a) {
return 1;;
return 1;
} else {
return 0;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ console.log(myArray2);

function loanCalculator (principle: number, interestRate: number, months = 12): string {
let interest: number = interestRate / 1200; // Calculates the monthly interest rate
let payment; Number;
let payment: Number;
payment = principle * interest / (1 - (Math.pow(1/(1 + interest), months)));
return payment.toFixed(2);
}
Expand Down