Function: Print Task Summary
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 49 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
listTasksByStatusfunction - The
initialTasks,updatedTasks,testTasks,progressTasks,completedTasks,mixedTasks,todoTasks,inProgressTasks, anddoneTasksarrays
Create a function named printTaskSummary that takes one parameter:
taskof typeTask(a single Task object)
The function should:
- Print a formatted summary to the console in the exact format:
"ID: [id], Title: [title], Status: [status]" - Have a return type of
void
Create a function named printAllTaskSummaries that takes one parameter:
taskListof typeTask[](an array of Task objects)
The function should:
- Use a loop to call
printTaskSummaryfor each task in the array - Have a return type of
void
Create a variable named sampleTasks of type Task[] containing the following tasks:
- A task with id
101, title"Design user interface", and status'todo' - A task with id
102, title"Implement authentication", and status'in-progress' - A task with id
103, title"Deploy to production", and status'done'
Print the following outputs:
- Call
printTaskSummarywithfirstTask - Call
printTaskSummarywith the second task fromsampleTasks - Call
printAllTaskSummarieswithsampleTasks - Call
printTaskSummarywith the last task fromdoneTasks
Try it yourself
// Task interface and previous code from earlier 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];
}
function changeTaskStatus(taskList: Task[], taskId: number, newStatus: 'todo' | 'in-progress' | 'done'): Task[] {
return taskList.map(task =>
task.id === taskId ? { ...task, status: newStatus } : task
);
}
const initialTasks: Task[] = [firstTask, secondTask];
const updatedTasks: Task[] = addTask(initialTasks, "Review code changes");
const testTasks: Task[] = [firstTask, secondTask, thirdTask];
const progressTasks = changeTaskStatus(testTasks, 1, 'in-progress');
const completedTasks = changeTaskStatus(progressTasks, 2, 'done');
// Create the listTasksByStatus function
function listTasksByStatus(taskList: Task[], status: 'todo' | 'in-progress' | 'done'): Task[] {
return taskList.filter(task => task.status === status);
}
// Create mixedTasks array
const mixedTasks: Task[] = [firstTask, secondTask, thirdTask];
// Filter tasks by status
const todoTasks = listTasksByStatus(mixedTasks, 'todo');
const inProgressTasks = listTasksByStatus(mixedTasks, 'in-progress');
const doneTasks = listTasksByStatus(mixedTasks, 'done');
// Print the required outputs
console.log(mixedTasks.length);
console.log(todoTasks.length);
console.log(inProgressTasks.length);
console.log(doneTasks.length);
console.log(getTaskInfo(todoTasks[0]));
console.log(getTaskInfo(inProgressTasks[0]));
console.log(getTaskInfo(doneTasks[0]));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