Menu
Coddy logo textTech

Putting It All Together

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 50 of 73.

challenge icon

Challenge

Easy

You are provided with the following from the previous challenge:

  • The Task interface with id (number), title (string), and status (literal type)
  • Three task variables: firstTask, secondTask, and thirdTask
  • The getTaskInfo function
  • The addTask function
  • The changeTaskStatus function
  • The listTasksByStatus function
  • The printTaskSummary function
  • The printAllTaskSummaries function
  • All task arrays: initialTasks, updatedTasks, testTasks, progressTasks, completedTasks, mixedTasks, todoTasks, inProgressTasks, doneTasks, and sampleTasks

Create a comprehensive task management workflow that demonstrates all the functionality you've built:

Create a variable named projectTasks of type Task[] containing these initial tasks:

  • A task with id 201, title "Setup development environment", and status 'done'
  • A task with id 202, title "Create project structure", and status 'todo'

Perform the following operations in sequence:

  1. Use addTask to add a new task with title "Write documentation" to projectTasks and store the result in expandedTasks
  2. Use changeTaskStatus to change the status of task with ID 202 to 'in-progress' in expandedTasks and store the result in updatedProjectTasks
  3. Use changeTaskStatus to change the status of the newly added task (ID 3) to 'done' in updatedProjectTasks and store the result in finalTasks
  4. Use listTasksByStatus to filter finalTasks for 'done' status and store the result in completedProjectTasks

Print the following outputs on separate lines:

  • Print the length of projectTasks
  • Print the length of finalTasks
  • Print the length of completedProjectTasks
  • Call printTaskSummary with the first task from completedProjectTasks
  • Call printTaskSummary with the last task from completedProjectTasks
  • Call printAllTaskSummaries with finalTasks

Try it yourself

// Task interface and existing code 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];
}

function changeTaskStatus(taskList: Task[], taskId: number, newStatus: 'todo' | 'in-progress' | 'done'): Task[] {
  return taskList.map(task => 
    task.id === taskId ? { ...task, status: newStatus } : task
  );
}

function listTasksByStatus(tasks: Task[], status: 'todo' | 'in-progress' | 'done'): Task[] {
    return tasks.filter(task => task.status === status);
}

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');
const mixedTasks: Task[] = [firstTask, secondTask, thirdTask];

const todoTasks = listTasksByStatus(mixedTasks, 'todo');
const inProgressTasks = listTasksByStatus(mixedTasks, 'in-progress');
const doneTasks = listTasksByStatus(mixedTasks, 'done');

function printTaskSummary(task: Task): void {
    console.log(`ID: ${task.id}, Title: ${task.title}, Status: ${task.status}`);
}

function printAllTaskSummaries(taskList: Task[]): void {
    for (let task of taskList) {
        printTaskSummary(task);
    }
}

const sampleTasks: Task[] = [
    { id: 101, title: "Design user interface", status: 'todo' },
    { id: 102, title: "Implement authentication", status: 'in-progress' },
    { id: 103, title: "Deploy to production", status: 'done' }
];

printTaskSummary(firstTask);
printTaskSummary(sampleTasks[1]);
printAllTaskSummaries(sampleTasks);
printTaskSummary(doneTasks[doneTasks.length - 1]);

All lessons in Introduction To TypeScript