Here's an example of a simple todo list in JavaScript:
// Create an empty array to store the todo items
let todoList = [];
// Function to add a new todo item to the list
function addTodoItem(item) {
todoList.push(item);
}
// Function to remove a todo item from the list
function removeTodoItem(index) {
if (index >= 0 && index < todoList.length) {
todoList.splice(index, 1);
}
}
// Function to display the todo list
function displayTodoList() {
console.log("Todo List:");
for (let i = 0; i < todoList.length; i++) {
console.log(`${i + 1}. ${todoList[i]}`);
}
}
// Example usage:
addTodoItem("Buy groceries");
addTodoItem("Do laundry");
addTodoItem("Clean the house");
displayTodoList(); // Display the initial todo list
removeTodoItem(1); // Remove the second item from the list
displayTodoList(); // Display the updated todo list
In this code, we create an empty array todoList to store the todo items. The addTodoItem function takes an item as input and pushes it to the array. The removeTodoItem function takes an index as input and removes the corresponding item from the array using the splice method. The displayTodoList function iterates over the array and displays each item with its corresponding index.
In the example usage, we add three items to the todo list, display the initial list, remove the second item, and display the updated list. You can modify the code according to your requirements to add more functionality or customize the user interface.