Lab - Convert three TypeScript functions to a class definition
In this lab, you'll apply what you've learned about classes to convert a TypeScript function to a class.
Exercise 1: Convert three TypeScript functions to a class definition
The following TypeScript 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.sortDecending
andsortAscending
are comparison functions that tell thesort()
method how to sort numbers in ascending or descending order.
Convert the functions to a TypeScript class.
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-05/m05-start code .
Open the file module05.ts. This file contains an empty class named
BuildArray
and thebuildArray
,sortDecending
, andsortAscending
functions.Locate
TODO Define the properties
.Define the properties in the class:
_items
and_sortOrder
.// TODO Define the properties private _items: number; private _sortOrder: 'ascending' | 'descending';
Locate
TODO Define the constructor
.Define the
constructor
for the properties.// TODO Define the constructor constructor (items: number, sortOrder: 'ascending' | 'descending') { this._items = items; this._sortOrder = sortOrder; }
Locate
TODO Define the accessors
.Define the accessors that
get
andset
the value of theitems
andsortOrder
parameters.// TODO Define the accessors get items() { return this._items; } set items(items) { this._items = items; } get sortOrder() { return this._sortOrder; } set sortOrder(sortOrder) { this._sortOrder = sortOrder; }
Locate
TODO Define the methods
.Move the
sortAscending
andsortDescending
functions into the class and make them bothprivate
methods of the class.// TODO Define the methods. private sortDescending = (a: number, b: number) => { if (a > b) { return -1; } else if (b > a) { return 1; } else { return 0;} } private sortAscending = (a: number, b: number) => { if (a > b) { return 1; } else if (b > a) { return -1; } else { return 0; } }
Move the
buildArray
function into the class and make it apublic
method of the class.buildArray(): number[] { let randomNumbers: number[] = []; let nextNumber: number; for (let counter = 0; counter < this.items; counter++) { nextNumber = Math.ceil(Math.random() * (100 - 1)); if (randomNumbers.indexOf(nextNumber) === -1) { randomNumbers.push(nextNumber); } else { counter--; } } if (this._sortOrder === 'ascending') { return randomNumbers.sort(this.sortAscending); } else { return randomNumbers.sort(this.sortDescending); } }
Locate
TODO: Instantiate the BuildArray objects
.Update the
testArray1
andtestArray2
variable declarations to instantiate newBuildArray
objects.let testArray1 = new BuildArray(12, 'ascending'); let testArray2 = new BuildArray(8, 'descending');
Test your work by calling the
buildArray
method on the objects and return the results to the console.console.log(testArray1.buildArray()); console.log(testArray2.buildArray());
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 classes. 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 ../m05-end
code .
Open the file module05.ts to see the solution to this lab. See the Lab setup section above for more information about setting up your development environment to run the solution.