gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
generate a random js code.
- User
generate a random js code.
- Assistant
Sure! Here's a random JavaScript code snippet that demonstrates a simple To-Do List application:
// To-Do List Application // Array to store todo items let todoList = []; // Function to add a todo item function addTodo(item) { if (item) { todoList.push(item); console.log(`Added: "${item}"`); } else { console.log("Invalid item"); } } // Function to remove a todo item by index function removeTodo(index) { if (index >= 0 && index < todoList.length) { let removedItem = todoList.splice(index, 1); console.log(`Removed: "${removedItem}"`); } else { console.log("Invalid index"); } } // Function to display all todo items function displayTodos() { if (todoList.length === 0) { console.log("Your to-do list is empty!"); } else { console.log("To-Do List:"); todoList.forEach((item, index) => { console.log(`${index + 1}. ${item}`); }); } } // Example usage: addTodo("Buy groceries"); addTodo("Read a book"); displayTodos(); removeTodo(0); displayTodos();This code defines a simple to-do list with functionalities to add, remove, and display tasks. It's a basic example to illustrate working with arrays and functions in JavaScript.