Function to Add a Task
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 46 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
Create a function named addTask that takes two parameters:
taskListof typeTask[](an array of Task objects)titleof typestring(the title for the new task)
The function should:
- Create a new task with a unique ID (use the length of the current array + 1)
- Set the title to the provided title parameter
- Set the status to
'todo' - Return a new array containing all existing tasks plus the new task
Create an initial task list by creating a variable named initialTasks of type Task[] containing firstTask and secondTask.
Use your addTask function to add a new task with the title "Review code changes" to initialTasks and store the result in a variable named updatedTasks.
Print the following outputs on separate lines:
- Print the length of
initialTasks - Print the length of
updatedTasks - Call
getTaskInfowith the last task inupdatedTasksand print the result - Print the title of the newly added task (the last task in
updatedTasks) - Print the status of the newly added task
Try it yourself
// TODO: Write your code here
// Create the Task interface
interface Task {
id: number;
title: string;
status: 'todo' | 'in-progress' | 'done';
}
// Create the task variables
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'
};
// Create the getTaskInfo function
function getTaskInfo(task: Task): string {
return `Task ${task.id}: ${task.title} - ${task.status}`;
}
// Print the required outputs
console.log(getTaskInfo(firstTask));
console.log(getTaskInfo(secondTask));
console.log(getTaskInfo(thirdTask));
console.log(firstTask.status);
console.log(secondTask.title);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