@@ -54,6 +54,29 @@ function isValidTaskDescription(description) {
5454 return description !== '' && description . length >= 3 ;
5555}
5656
57+ /**
58+ * Removes a task
59+ * @param {number } taskId - The ID of the task to remove
60+ */
61+ function removeTask ( taskId ) {
62+ // Find all tasks except the one to remove
63+ var newTasks = [ ] ;
64+ for ( var i = 0 ; i < tasks . lenght ; i ++ ) {
65+ if ( tasks [ i ] . id !== taskId ) {
66+ newTasks . push ( tasks [ i ] ) ;
67+ }
68+ }
69+
70+ // Update tasks array
71+ tasks = newTasks ;
72+
73+ // Log the change
74+ console . log ( 'Task removed!' ) ;
75+
76+ // Re-render tasks to update the UI
77+ renderTasks ( ) ;
78+ }
79+
5780/**
5881 * Creates a new task object
5982 * @param {string } description - The task description
@@ -68,8 +91,7 @@ function createTaskObject(description, priority, date) {
6891 priority : priority ,
6992 date : date ,
7093 completed : false
71- }
72-
94+ } ;
7395}
7496
7597/**
@@ -78,18 +100,26 @@ function createTaskObject(description, priority, date) {
78100 * @returns {HTMLElement } - The created task list item
79101 */
80102function renderTaskElement ( taskData ) {
81- // Crete the list item element
103+ // Create the list item element
82104 var li = document . createElement ( 'li' ) ;
83105 li . className = 'task-item' ;
84106
107+ // Add data-id attribute to connect element with task data
108+ li . setAttribute ( 'data-id' , taskData . id ) ;
85109
86110 // Add the priority class
87111 li . classList . add ( 'task-item-' + taskData . priority ) ;
88112
89- // Create and add the checkbox
113+ // Add completed class if task is completed
114+ if ( taskData . completed ) {
115+ li . classList . add ( 'task-completed' ) ;
116+ }
117+
118+ // Create and add the checkbox with checked status
90119 var checkbox = document . createElement ( 'input' ) ;
91120 checkbox . type = 'checkbox' ;
92121 checkbox . className = 'task-checkbox' ;
122+ checkbox . checked = taskData . completed ;
93123 li . appendChild ( checkbox ) ;
94124
95125 // Create and add content container
@@ -150,6 +180,9 @@ function handleFormSubmit(e) {
150180 // Render the new task
151181 renderTasks ( ) ;
152182
183+ console . log ( 'Task added:' , newTask ) ;
184+ console . log ( 'Total tasks:' , tasks . length ) ;
185+
153186 // Reset form
154187 taskForm . reset ( ) ;
155188 }
@@ -171,6 +204,73 @@ function renderTasks() {
171204 updateTaskCount ( ) ;
172205}
173206
207+ /**
208+ * Finds a task by its ID
209+ * @param {number } taskId - The ID to search for
210+ * @returns {Object|null } - The found task or null
211+ */
212+ function findTaskById ( taskId ) {
213+ for ( var i = 0 ; i < tasks . length ; i ++ ) {
214+ if ( tasks [ i ] . id === taskId ) {
215+ return tasks [ i ] ;
216+ }
217+ }
218+ return null ;
219+ }
220+
221+ /**
222+ * Toggles a task's completion status
223+ * @param {number } taskId - The ID of the task toggle
224+ */
225+ function toggleTaskComplete ( taskId ) {
226+ // Find task by ID
227+ var task = findTaskById ( taskId ) ;
228+
229+ if ( task ) {
230+ // Toggle completion status
231+ task . completed = ! task . completed ;
232+
233+ // Log the change
234+ console . log ( task . completed ? 'Task marked complete!' : 'Task marked incomplete!' ) ;
235+
236+ // Re-render tasks to update the UI
237+ renderTasks ( ) ;
238+ }
239+ }
240+
241+ /**
242+ * Handle task list interactions
243+ * @param {Event } e - The click event
244+ */
245+ function handleTaskListClick ( e ) {
246+ console . log ( 'Task list clicked:' , e . target ) ;
247+
248+ // Find the closest task item
249+ var taskItem = e . target ;
250+ while ( taskItem && ! taskItem . classList . contains ( 'task-item' ) ) {
251+ taskItem = taskItem . parentElement ;
252+ }
253+
254+ // Return if no task item found
255+ if ( ! taskItem ) return ;
256+
257+ // Get task ID
258+ var taskId = parseInt ( taskItem . getAttribute ( 'data-id' ) ) ;
259+ console . log ( 'Clicked task ID:' , taskId ) ;
260+
261+ // Check which element was clicked
262+ if ( e . target . classList . contains ( 'task-checkbox' ) ) {
263+ // Checkbox clicked
264+ toggleTaskComplete ( taskId ) ;
265+ } else if ( e . target . classList . contains ( 'delete-btn' ) ) {
266+ // Delete button clicked
267+ if ( confirm ( 'Are you sure you want to delete this task?' ) ) {
268+ removeTask ( taskId ) ;
269+ }
270+ }
271+ }
272+
273+
174274/**
175275 * Handle filter button clicks
176276 * @param {Event } e - The click event
@@ -218,6 +318,7 @@ function initApp() {
218318 // Add event listeners
219319 taskForm . addEventListener ( 'submit' , handleFormSubmit ) ;
220320 document . querySelector ( 'nav' ) . addEventListener ( 'click' , handleFilterClick ) ;
321+ taskList . addEventListener ( 'click' , handleTaskListClick ) ;
221322
222323 // Render tasks (will be empty intially)
223324 renderTasks ( ) ;
0 commit comments