Learn how to create a dynamic to-do list using JavaScript arrays and functions. This guide covers adding, removing, and displaying tasks, with practical code examples and activities.
Creating a to-do list is a fantastic way to apply your JavaScript skills to a real-world project. This simple application will help you understand how to work with arrays, manage user input, and manipulate data dynamically. By the end of this section, you’ll have a fully functional to-do list that you can expand and personalize.
Our to-do list will have several key features:
Let’s start by creating the basic structure of our to-do list application. We’ll use an array to store our tasks and write functions to manage these tasks.
We’ll use a simple array to keep track of our tasks. This array will hold strings, each representing a task.
let toDoList = [];
To add a task to our list, we’ll create a function called addTask
. This function will take a string as an argument and add it to our toDoList
array.
function addTask(task) {
toDoList.push(task);
console.log(`'${task}' has been added to your list.`);
}
Next, we’ll create a function to remove tasks from our list. The removeTask
function will search for the task in the array and remove it if found.
function removeTask(task) {
let index = toDoList.indexOf(task);
if (index > -1) {
toDoList.splice(index, 1);
console.log(`'${task}' has been removed from your list.`);
} else {
console.log(`'${task}' not found in your to-do list.`);
}
}
Finally, we’ll create a function to display all tasks in our list. The listTasks
function will iterate over the toDoList
array and print each task.
function listTasks() {
console.log('Your To-Do List:');
toDoList.forEach(function(task, index) {
console.log(`${index + 1}. ${task}`);
});
}
Let’s see how these functions work together:
// Adding tasks
addTask('Complete homework');
addTask('Wash dishes');
// Listing tasks
listTasks();
// Removing a task
removeTask('Wash dishes');
// Listing tasks again
listTasks();
Now that we have a basic to-do list, let’s enhance it by adding a feature to mark tasks as completed. We’ll modify our array to store objects instead of strings, allowing us to track the completion status of each task.
We’ll change our toDoList
array to store objects with task
and completed
properties.
let toDoList = [];
addTask
FunctionUpdate the addTask
function to create an object for each task.
function addTask(task) {
toDoList.push({ task: task, completed: false });
console.log(`'${task}' has been added to your list.`);
}
markTaskCompleted
FunctionAdd a new function to mark a task as completed.
function markTaskCompleted(task) {
let taskObj = toDoList.find(t => t.task === task);
if (taskObj) {
taskObj.completed = true;
console.log(`'${task}' has been marked as completed.`);
} else {
console.log(`'${task}' not found in your to-do list.`);
}
}
listTasks
FunctionModify the listTasks
function to display the completion status of each task.
function listTasks() {
console.log('Your To-Do List:');
toDoList.forEach(function(taskObj, index) {
const status = taskObj.completed ? '[x]' : '[ ]';
console.log(`${index + 1}. ${status} ${taskObj.task}`);
});
}
Here’s how you can use the enhanced to-do list:
// Adding tasks
addTask('Complete homework');
addTask('Wash dishes');
// Listing tasks
listTasks();
// Marking a task as completed
markTaskCompleted('Complete homework');
// Listing tasks again
listTasks();
removeTask
and markTaskCompleted
functions.Creating a to-do list is a practical exercise that reinforces your understanding of arrays, functions, and user interaction in JavaScript. This project can be expanded with additional features, such as due dates, priority levels, and categories, providing endless opportunities for learning and creativity.