Okay, let's do one more exercise to reinforce our learning before we move on to covering more types
Exercise 3: Arrays and Enums
Objective:
-
Practice defining and using arrays and enums in TypeScript.
-
Implement a function that processes an array and uses an enum to categorize data.
-
Create a file named
exercise3.ts
in yoursrc
directory. -
Open
exercise3.ts
in your text editor and complete the following code:
ts
// exercise3.ts// Define an enum for task status of 'NotStarted', 'InProgress' and 'Completed'?? TaskStatus {??}// Define an array of tasks each with a title and statuslet tasks: {???} = [// Add 3 sample tasks with statuses// 1. 'Learn TypeScript' - InProgress// 2. 'Build a TypeScript project' - NotStarted// 3. 'Review TypeScript concepts' - Completed]// Define a function that prints the tasks with their statusesfunction printTasks(???: {???}[]): ??? {tasks.forEach(task => {???console.log(??); // print the name and status});}// Call the function and log the tasksprintTasks(tasks);
Check Solution
- Compile and run your code
bash
npx tscnode ./dist/exercise3.js
Your output in the console should be:
bash
> node ./dist/exercise3.jsLearn TypeScript: In ProgressBuild a TypeScript project: Not StartedReview TypeScript concepts: Completed
Solution:
ts
// exercise3.ts// Define an enum for task status of 'NotStarted', 'InProgress' and 'Completed'enum TaskStatus {NotStarted = "NotStarted",InProgress = "InProgress",Completed = "Completed",}// Define an array of taskslet tasks: { title: string; status: TaskStatus }[] = [{ title: "Learn TypeScript", status: TaskStatus.InProgress },{ title: "Build a TypeScript project", status: TaskStatus.NotStarted },{ title: "Review TypeScript concepts", status: TaskStatus.Completed },];// Define a function that prints the tasks with their statusesfunction printTasks(tasks: { title: string; status: TaskStatus }[]) {tasks.forEach((task) => {console.log(`${task.title} - ${task.status}`); // print the name and status});}// Call the function and log the tasksprintTasks(tasks);