Lab - Use functions in TypeScript
In this lab, you'll convert some JavaScript functions to TypeScript.
Exercise 1: Convert a JavaScript function to TypeScript with strongly typed parameters and return types
The following JavaScript code contains three functions:
BuildArray
builds an array of unique random numbers. It accepts anitems
parameter that determines the number of items in the array and asortOrder
parameter that determines whether the array is sorted in ascending or descending order.sortDescending
andsortAscending
are comparison functions that tell thesort()
method how to sort numbers in ascending or descending order.
Convert the functions to TypeScript with strongly typed parameters and return types.
Clone the starting repository by entering the following at the command prompt.
git clone https://github.com/MicrosoftDocs/mslearn-typescript cd mslearn-typescript/code/module-04/m04-start code .
Open the file module04.ts.
Locate
TODO: Update the BuildArray function
.In the
BuildArray
function, add types to the parameter list, return value, and variables.function buildArray(items: number, sortOrder: 'ascending' | 'descending'): number[] { let randomNumbers: number[] = []; let nextNumber: number; for (let counter = 0; counter < items; counter++) { nextNumber = Math.ceil(Math.random() * (100 - 1)); if (randomNumbers.indexOf(nextNumber) === -1) { randomNumbers.push(nextNumber); } else { counter--; } } if (sortOrder === 'ascending') { return randomNumbers.sort(sortAscending); } else { return randomNumbers.sort(sortDescending); } }
Locate
TODO: Convert the sortDescending and sortAscending functions to arrow functions
.Convert the
sortDescending
andsortAscending
functions to anonymous functions and assign them to variables of the same name.let sortDescending = (a, b) => { if (a > b) { return -1; } else if (b > a) { return 1; } else { return 0; } } let sortAscending = (a, b) => { if (a > b) { return 1; } else if (b > a) { return -1; } else { return 0; } }
Locate
TODO: Declare a new function type for the sortDescending and sortAscending functions
.Declare a new function type for the
sortDescending
andsortAscending
functions using either a type alias or an interface.type compareFunctionType = (a: number, b:number) => number;
In the variable declarations for
sortDescending
andsortAscending
, apply the new function type as the variable type.let sortDescending: compareFunctionType = (a, b) => { if (a > b) { return -1; } else if (b > a) { return 1; } else { return 0; } } let sortAscending: compareFunctionType = (a, b) => { if (a > b) { return 1; } else if (b > a) { return -1; } else { return 0; } }
Test your work by calling the
buildArray
function.let myArray1 = buildArray(12, 'ascending'); let myArray2 = buildArray(8, 'descending'); console.log(myArray1); console.log(myArray2);
Exercise 2: Convert a JavaScript function to a TypeScript using optional parameters
This JavaScript function returns the payment amount for a loan.
Locate
TODO: Update the LoanCalculator function
.Convert the
loanCalculator
function to TypeScript with strongly typed parameters, variables, and return types.Make the
months
parameter optional but assign it a default value of12
months if omitted.function loanCalculator (principle: number, interestRate: number, months = 12): string { let interest: number = interestRate / 1200; // Calculates the monthly interest rate let payment: number; payment = principle * interest / (1 - (Math.pow(1/(1 + interest), months))); return payment.toFixed(2); }
Test your work by calling the
loanCalculator
function with only the required parameters.let myLoan = loanCalculator(1000, 5); console.log(myLoan);
Challenge yourself!
For an added challenge, take some existing JavaScript that you may have written or that you find on the web and rewrite it in TypeScript using what you've learned about functions. You can copy and paste the JavaScript into the Playground and edit it or use another editor like Visual Studio Code.
Lab solution
View the final version of the code by entering the following at the command prompt.
cd ../m04-end
code .
Open the file module04.ts to see the solution to this lab.