Function: List Tasks by Status
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 48 of 73.
Challenge
EasyYou are provided with the following from the previous challenge:
- The
Taskinterface withid(number),title(string), andstatus(literal type) - Three task variables:
firstTask,secondTask, andthirdTask - The
getTaskInfofunction - The
addTaskfunction - The
changeTaskStatusfunction - The
initialTasks,updatedTasks,testTasks,progressTasks, andcompletedTasksarrays
Create a function named listTasksByStatus that takes two parameters:
taskListof typeTask[](an array of Task objects)statusof type'todo' | 'in-progress' | 'done'(the status to filter by)
The function should:
- Use the array
filtermethod to return a new array containing only tasks that match the specified status - Return the filtered array of tasks
Create a variable named mixedTasks of type Task[] containing all three original tasks: firstTask, secondTask, and thirdTask.
Use your listTasksByStatus function to create the following filtered arrays:
- Filter
mixedTasksfor'todo'status and store the result intodoTasks - Filter
mixedTasksfor'in-progress'status and store the result ininProgressTasks - Filter
mixedTasksfor'done'status and store the result indoneTasks
Print the following outputs on separate lines:
- Print the length of
mixedTasks - Print the length of
todoTasks - Print the length of
inProgressTasks - Print the length of
doneTasks - Call
getTaskInfowith the first task fromtodoTasksand print the result - Call
getTaskInfowith the first task frominProgressTasksand print the result - Call
getTaskInfowith the first task fromdoneTasksand print the result
Try it yourself
// Task interface and variables from previous challenges
interface Task {
id: number;
title: string;
status: 'todo' | 'in-progress' | 'done';
}
const firstTask: Task = {
id: 1,
title: "Learn TypeScript interfaces",
status: 'todo'
};
const secondTask: Task = {
id: 2,
title: "Build task management app",
status: 'in-progress'
};
const thirdTask: Task = {
id: 3,
title: "Write unit tests",
status: 'done'
};
function getTaskInfo(task: Task): string {
return `Task ${task.id}: ${task.title} - ${task.status}`;
}
function addTask(taskList: Task[], title: string): Task[] {
const newTask: Task = {
id: taskList.length + 1,
title: title,
status: 'todo'
};
return [...taskList, newTask];
}
const initialTasks: Task[] = [firstTask, secondTask];
const updatedTasks: Task[] = addTask(initialTasks, "Review code changes");
// Create the changeTaskStatus function
function changeTaskStatus(taskList: Task[], taskId: number, newStatus: 'todo' | 'in-progress' | 'done'): Task[] {
return taskList.map(task =>
task.id === taskId ? { ...task, status: newStatus } : task
);
}
// Create testTasks array
const testTasks: Task[] = [firstTask, secondTask, thirdTask];
// Use changeTaskStatus function
const progressTasks = changeTaskStatus(testTasks, 1, 'in-progress');
const completedTasks = changeTaskStatus(progressTasks, 2, 'done');
// Print the required outputs
console.log(getTaskInfo(testTasks[0]));
console.log(getTaskInfo(progressTasks[0]));
console.log(getTaskInfo(completedTasks[1]));
console.log(testTasks[0].status);
console.log(completedTasks[1].status);All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs7Project: A Simple Task List
Project: Defining Task StructFunction to Add a Task2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples