Advertisement
SkidScripts

Untitled

Nov 29th, 2023
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Define an array to store tasks
  2. let tasks = [];
  3.  
  4. // Function to add a task to the list
  5. function addTask(task) {
  6.     tasks.push(task);
  7.     console.log(`Task "${task}" added.`);
  8. }
  9.  
  10. // Function to remove a task from the list
  11. function removeTask(task) {
  12.     const index = tasks.indexOf(task);
  13.     if (index !== -1) {
  14.         tasks.splice(index, 1);
  15.         console.log(`Task "${task}" removed.`);
  16.     } else {
  17.         console.log(`Task "${task}" not found.`);
  18.     }
  19. }
  20.  
  21. // Function to display all tasks
  22. function displayTasks() {
  23.     if (tasks.length === 0) {
  24.         console.log('Task list is empty.');
  25.     } else {
  26.         console.log('Tasks:');
  27.         tasks.forEach((task, index) => {
  28.             console.log(`${index + 1}. ${task}`);
  29.         });
  30.     }
  31. }
  32.  
  33. // Add tasks to the list
  34. addTask('Complete homework');
  35. addTask('Buy groceries');
  36. addTask('Call friend');
  37.  
  38. // Display initial list of tasks
  39. displayTasks();
  40.  
  41. // Remove a task from the list
  42. removeTask('Buy groceries');
  43.  
  44. // Display updated list of tasks
  45. displayTasks();
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement