Menu
Coddy logo textTech

Function: Print Task Summary

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 49 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 initialTasks, updatedTasks, testTasks, progressTasks, completedTasks, mixedTasks, todoTasks, inProgressTasks, and doneTasks arrays

Create a function named printTaskSummary that takes one parameter:

  • task of type Task (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:

  • taskList of type Task[] (an array of Task objects)

The function should:

  • Use a loop to call printTaskSummary for 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 printTaskSummary with firstTask
  • Call printTaskSummary with the second task from sampleTasks
  • Call printAllTaskSummaries with sampleTasks
  • Call printTaskSummary with the last task from doneTasks

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