Function to Change Task Status
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 47 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
initialTasksandupdatedTasksarrays
Create a function named changeTaskStatus that takes three parameters:
taskListof typeTask[](an array of Task objects)taskIdof typenumber(the ID of the task to update)newStatusof type'todo' | 'in-progress' | 'done'(the new status for the task)
The function should:
- Find the task with the matching ID in the task list
- Update that task's status to the new status
- Return a new array with the updated task (do not modify the original array)
- If no task with the given ID is found, return the original array unchanged
Create a variable named testTasks of type Task[] containing firstTask, secondTask, and thirdTask.
Use your changeTaskStatus function to:
- Change the status of task with ID
1to'in-progress'and store the result inprogressTasks - Change the status of task with ID
2to'done'inprogressTasksand store the result incompletedTasks
Print the following outputs on separate lines:
- Call
getTaskInfowith the first task fromtestTasksand print the result - Call
getTaskInfowith the first task fromprogressTasksand print the result - Call
getTaskInfowith the second task fromcompletedTasksand print the result - Print the status of the first task in
testTasks - Print the status of the second task in
completedTasks
Try it yourself
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");
console.log(initialTasks.length);
console.log(updatedTasks.length);
console.log(getTaskInfo(updatedTasks[updatedTasks.length - 1]));
console.log(updatedTasks[updatedTasks.length - 1].title);
console.log(updatedTasks[updatedTasks.length - 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