Build a Task Tracker with Priority Sorting
Create a JavaScript function to manage and sort a list of tasks with priorities and deadlines. This mini-project simulates a simple task tracker with sorting and filtering.
Challenge prompt
Write a function named `manageTasks` that accepts two parameters: an array of task objects and an options object. Each task object has `id`, `title`, `priority` (integer from 1 to 5, 1 being highest), and `dueDate` (ISO date string). The options object may specify `sortBy` (either 'priority' or 'dueDate'), and an optional filter `minPriority` to only include tasks with priority less than or equal to that value (e.g., minPriority=3 means include priority 1, 2, or 3). The function should filter and then sort the tasks accordingly and return the final array of tasks. Example usage: manageTasks(tasks, { sortBy: 'dueDate', minPriority: 3 }) should return tasks with priority 1, 2, or 3, sorted by dueDate ascending. Implement robust comparison and filtering logic inside your function.
Guidance
- • First, filter the tasks based on the minPriority if provided.
- • Next, sort the filtered list by the chosen key: priority or dueDate.
- • Remember that priority sorting is ascending (1 is highest), and dueDate sorting is ascending (earliest date first).
- • Ensure that dueDate strings are converted to Date objects for accurate comparison.
Hints
- • Use Array.filter() to implement the filtering by priority.
- • Use Array.sort() with a custom comparator function for sorting.
- • The Date.parse() or new Date() constructor can help convert date strings for comparison.
Starter code
function manageTasks(tasks, options) {
// Your code here
}Expected output
[ { id: 5, title: 'Finish report', priority: 2, dueDate: '2024-06-10' }, { id: 3, title: 'Update docs', priority: 3, dueDate: '2024-06-15' } ]
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.