Task List Utilities

Write task list helpers that return updated arrays.

Start building, submit solution and get feedback from the community.
2Submit Solution
5 upvotes10 upvotes

Task List Utilities

You are writing helper functions for a task list. Each helper should return a result without mutating the original tasks array.

Write these functions:

  • addTask(tasks, title) should return a new array with a new incomplete task added.

  • completeTask(tasks, taskId) should return a new array where only the matching task is completed.

  • removeTask(tasks, taskId) should return a new array without the matching task.

  • countIncompleteTasks(tasks) should return the number of incomplete tasks.

Sample checks:

js
const tasks = [  { id: 1, title: 'Review variables', completed: true },  { id: 2, title: 'Practice functions', completed: false },];const withNewTask = addTask(tasks, 'Build task utilities');console.log(withNewTask.map((task) => task.title));const completed = completeTask(withNewTask, 2);console.log(countIncompleteTasks(completed));console.log(removeTask(completed, 1).map((task) => task.id));console.log(tasks.length);console.log(countIncompleteTasks(tasks));

Expected output:

txt
["Review variables", "Practice functions", "Build task utilities"]1[2, 3]21

The original tasks array should still have two items after these helper functions run.

Solution

Solution:

js
function addTask(tasks, title) {  const ids = tasks.map((task) => task.id);  const nextId = Math.max(...ids) + 1;  const newTask = { id: nextId, title, completed: false };  return [...tasks, newTask];}function completeTask(tasks, taskId) {  return tasks.map((task) => {    if (task.id !== taskId) {      return task;    }    return { ...task, completed: true };  });}function removeTask(tasks, taskId) {  return tasks.filter((task) => task.id !== taskId);}function countIncompleteTasks(tasks) {  return tasks.filter((task) => task.completed === false).length;}const tasks = [  { id: 1, title: 'Review variables', completed: true },  { id: 2, title: 'Practice functions', completed: false },];const withNewTask = addTask(tasks, 'Build task utilities');console.log(withNewTask.map((task) => task.title));const completed = completeTask(withNewTask, 2);console.log(countIncompleteTasks(completed));console.log(removeTask(completed, 1).map((task) => task.id));console.log(tasks.length);console.log(countIncompleteTasks(tasks));

Join the Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Rank  out of 28M!

357K

GitHub Stars

Star us on GitHub
Help us reach #1

+90kevery month

+2.8M

Registered Users

Register yourself
Commit to your growth

+2kevery month

48K

Discord Members

Join on Discord
Join the community

RoadmapsGuidesFAQsYouTube

roadmap.shby@nilbuild

Community created roadmaps, best practices, projects, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh·Terms·Privacy·

ThewNewStack

The top DevOps resource for Kubernetes, cloud-native computing, and large-scale development and deployment.