Here’s a simple example of how you could create a to-do list in JavaScript:
// Create an array to store the to-do items
let toDoList = [];
// Add a new item to the to-do list
function addToDo(task) {
toDoList.push(task);
}
// Remove a completed item from the to-do list
function completeToDo(index) {
toDoList.splice(index, 1);
}
// Display the to-do list
function displayToDoList() {
console.log("My To-Do List:");
toDoList.forEach((task, index) => {
console.log(`${index + 1}: ${task}`);
});
}
// Example usage:
addToDo("Take out the trash");
addToDo("Do the dishes");
addToDo("Vacuum the carpet");
displayToDoList();
// Output:
// My To-Do List:
// 1: Take out the trash
// 2: Do the dishes
// 3: Vacuum the carpet
completeToDo(1);
displayToDoList();
// Output:
// My To-Do List:
// 1: Take out the trash
// 2: Vacuum the carpet
This example demonstrates the basic steps to create a to-do list:
- Create an array to store the to-do items.
- Write functions to add and remove items from the to-do list.
- Write a function to display the to-do list.
- Call the functions to add items to the to-do list and display the list.
+ There are no comments
Add yours