@@ -20,6 +20,30 @@ var taskList;
2020var itemsLeft ;
2121var filterButtons ;
2222
23+ /**
24+ * Gets today's date formatted for HTML date input (YYYY-MM-DD)
25+ */
26+ function getFormattedToday ( ) {
27+ var today = new Date ( ) ;
28+
29+ // Get year, month and dy components
30+ var year = today . getFullYear ( ) ;
31+ var month = today . getMonth ( ) + 1 ; // Months are 0-based
32+ var day = today . getDate ( ) ;
33+
34+ // Pad month and day with leading zeros if needed
35+ if ( month < 10 ) {
36+ month = '0' + month ;
37+ }
38+
39+ if ( day < 10 ) {
40+ day = '0' + day ;
41+ }
42+
43+ // Return formatted string (YYYY-MM-DD)
44+ return year + '-' + month + '-' + day ;
45+ }
46+
2347/**
2448 * Updates the document title with task count
2549 */
@@ -46,12 +70,32 @@ function updateTaskCount() {
4670}
4771
4872/**
49- * Validates task description
50- * @param {string } description - The task description to validate
51- * @returns {boolean } - Whether the description is valid
73+ * Validates a task to ensure it meets requirements
74+ * @param {string } description - The task description
75+ * @param {string } date - The due date
76+ * @returns {Array } - Array of error messages (empty if valid)
5277 */
53- function isValidTaskDescription ( description ) {
54- return description !== '' && description . length >= 3 ;
78+ function validateTask ( description , date ) {
79+ var errors = [ ] ;
80+
81+ // Description validation
82+ if ( ! description ) {
83+ errors . push ( 'Task description is required' ) ;
84+ } else if ( description . length < 3 ) {
85+ errors . push ( 'Task description must be at least 3 characters long' ) ;
86+ }
87+
88+ // Date validation
89+ if ( ! date ) {
90+ errors . push ( 'Due date is required' ) ;
91+ } else {
92+ var today = getFormattedToday ( ) ;
93+ if ( date < today ) {
94+ errors . push ( 'Due date cannot be in the past' ) ;
95+ }
96+ }
97+
98+ return errors ;
5599}
56100
57101/**
@@ -61,7 +105,7 @@ function isValidTaskDescription(description) {
61105function removeTask ( taskId ) {
62106 // Find all tasks except the one to remove
63107 var newTasks = [ ] ;
64- for ( var i = 0 ; i < tasks . lenght ; i ++ ) {
108+ for ( var i = 0 ; i < tasks . length ; i ++ ) {
65109 if ( tasks [ i ] . id !== taskId ) {
66110 newTasks . push ( tasks [ i ] ) ;
67111 }
@@ -77,6 +121,46 @@ function removeTask(taskId) {
77121 renderTasks ( ) ;
78122}
79123
124+ /**
125+ * Adds a new task
126+ * @param {string } description - The task description
127+ * @param {string } priority - The task priority
128+ * @param {string } date - The due date
129+ */
130+ function addTask ( description , priority , date ) {
131+ // Validate task
132+ var errors = validateTask ( description , date ) ;
133+ if ( errors . length > 0 ) {
134+ formError . textContent = errors . join ( '. ' ) ;
135+ formError . style . display = 'block' ;
136+ return false ;
137+ }
138+
139+ // Clear previous errors
140+ formError . textContent = '' ;
141+ formError . style . display = 'none' ;
142+
143+ // Create new task object
144+ var newTask = {
145+ id : Date . now ( ) ,
146+ description : description ,
147+ priority : priority ,
148+ date : date ,
149+ completed : false
150+ } ;
151+
152+ // Add to tasks array
153+ tasks . push ( newTask ) ;
154+
155+ // Render all tasks
156+ renderTasks ( ) ;
157+
158+ // Show confirmation
159+ console . log ( 'Task added successfully!' ) ;
160+
161+ return true ;
162+ }
163+
80164/**
81165 * Creates a new task object
82166 * @param {string } description - The task description
@@ -164,28 +248,32 @@ function handleFormSubmit(e) {
164248 var priority = taskPriority . value ;
165249 var date = taskDate . value ;
166250
167- // Validate description
168- if ( ! isValidTaskDescription ( description ) ) {
169- alert ( 'Please enter a valid task description (at least 3 characters)' ) ;
170- taskInput . focus ( ) ;
171- return ;
172- }
251+ // Add task
252+ var success = addTask ( description , priority , date ) ;
173253
174- // Create new task object
175- var newTask = createTaskObject ( description , priority , date ) ;
254+ // Reset form if successful
255+ if ( success ) {
256+ resetFormWithCleanup ( ) ;
257+ }
258+ }
176259
177- // Add to tasks array
178- tasks . push ( newTask ) ;
260+ /**
261+ * Resets the form and clears error messages
262+ */
263+ function resetFormWithCleanup ( ) {
264+ // Reset the form
265+ taskForm . reset ( ) ;
179266
180- // Render the new task
181- renderTasks ( ) ;
267+ // Set the date field back to today's date
268+ taskDate . value = getFormattedToday ( ) ;
182269
183- console . log ( 'Task added:' , newTask ) ;
184- console . log ( 'Total tasks:' , tasks . length ) ;
270+ // Clear any errors
271+ formError . textContent = '' ;
272+ formError . style . display = 'none' ;
185273
186- // Reset form
187- taskForm . reset ( ) ;
188- }
274+ // Focus back on the input
275+ taskInput . focus ( ) ;
276+ }
189277
190278/**
191279 * Renders all tasks in the tasks array
@@ -307,14 +395,21 @@ function initApp() {
307395 taskInput = document . getElementById ( 'task-input' ) ;
308396 taskPriority = document . getElementById ( 'task-priority' ) ;
309397 taskDate = document . getElementById ( 'task-date' ) ;
398+ formError = document . getElementById ( 'form-error' ) ;
399+
400+ // Hide the form error box initially
401+ formError . style . display = 'none' ;
310402
311403 // Select task list elements
312404 taskList = document . getElementById ( 'task-list' ) ;
313405 itemsLeft = document . getElementById ( 'items-left' ) ;
314406
315- // Select filter buttons
407+ // Select all filter buttons
316408 filterButtons = document . querySelectorAll ( '.filter-btn' ) ;
317409
410+ // Set default date to today
411+ taskDate . value = getFormattedToday ( ) ;
412+
318413 // Add event listeners
319414 taskForm . addEventListener ( 'submit' , handleFormSubmit ) ;
320415 document . querySelector ( 'nav' ) . addEventListener ( 'click' , handleFilterClick ) ;
0 commit comments