@@ -20,12 +20,91 @@ var taskList;
2020var itemsLeft ;
2121var filterButtons ;
2222
23- // Updates the document title with task count
23+ /**
24+ * Updates the document title with task count
25+ */
2426function updateDocumentTitle ( ) {
25- document . title = "KittyTasker - " + tasks . length + " Tasks" ;
27+ if ( tasks . length === 1 ) {
28+ document . title = "KittyTasker - " + tasks . length + " Task" ;
29+ } else {
30+ document . title = "KittyTasker - " + tasks . length + " Tasks" ;
31+ }
32+ }
33+
34+ /**
35+ * Updates the displayed task count
36+ */
37+ function updateTaskCount ( ) {
38+ itemsLeft . textContent = tasks . length ;
39+ updateDocumentTitle ( ) ;
2640}
2741
28- // Intitalise the application
42+ /**
43+ * Validates task description
44+ * @param {string } description - The task description to validate
45+ * @returns {boolean } - Whether the description is valid
46+ */
47+ function isValidTaskDescription ( description ) {
48+ return description !== '' && description . length >= 3 ;
49+ }
50+
51+ /**
52+ * Creates a new task object
53+ * @param {string } description - The task description
54+ * @param {string } priority - The task priority
55+ * @param {string } date - The due date for the task
56+ * @returns {Object } - The new task object
57+ */
58+ function createTaskObject ( description , priority , date ) {
59+ return {
60+ id : Date . now ( ) ,
61+ description : description ,
62+ priority : priority ,
63+ date : date ,
64+ completed : false
65+ }
66+
67+ }
68+
69+ /**
70+ * Handle task form submission
71+ * @param {Event } e - The form submission event
72+ */
73+ function handleFormSubmit ( e ) {
74+ // Prevent the default form submission
75+ e . preventDefault ( ) ;
76+
77+ // Get form data
78+ var description = taskInput . value . trim ( ) ;
79+ var priority = taskPriority . value ;
80+ var date = taskDate . value ;
81+
82+ // Validate description
83+ if ( ! isValidTaskDescription ( description ) ) {
84+ alert ( 'Please enter a valid task description (at least 3 characters)' ) ;
85+ taskInput . focus ( ) ;
86+ return ;
87+ }
88+
89+ // Create new task object
90+ var newTask = createTaskObject ( description , priority , date ) ;
91+
92+ // Add to tasks array
93+ tasks . push ( newTask ) ;
94+
95+ // Update the UI
96+ updateTaskCount ( ) ;
97+
98+ console . log ( 'Task added:' , newTask ) ;
99+ console . log ( 'Total tasks:' , tasks . length )
100+
101+ // Reset form
102+ taskForm . reset ( ) ;
103+ }
104+
105+ /**
106+ * Initialise the application
107+ */
29108function initApp ( ) {
30109 console . log ( 'Intialising KittyTasker app.' ) ;
31110
@@ -42,9 +121,8 @@ function initApp() {
42121 // Select filter buttons
43122 filterButtons = document . querySelectorAll ( '.filter-btn' ) ;
44123
45- // Log elements to verify selections
46- console . log ( 'Form element:' , taskForm ) ;
47- console . log ( 'Filter buttons:' , filterButtons ) ;
124+ // Add event listeners
125+ taskForm . addEventListener ( 'submit' , handleFormSubmit ) ;
48126
49127 // Update the document title
50128 updateDocumentTitle ( ) ;
0 commit comments