Complete the code to create an empty priority queue using an array.
const priorityQueue = [1];Priority queues can be implemented using arrays. Here, an empty array [] initializes the queue.
Complete the code to add an element with priority to the priority queue array.
priorityQueue.push({value: 'task1', priority: [1]);Priority is usually represented by a number. Lower numbers often mean higher priority. Here, 1 is used as a priority.
Fix the error in the code to remove the highest priority element (lowest number) from the queue.
priorityQueue.sort((a, b) => a.priority [1] b.priority);
const highestPriority = priorityQueue.shift();Sorting ascending by priority means smaller numbers come first. Use < to sort from lowest to highest.
Fill both blanks to check if the priority queue is empty and print a message.
if (priorityQueue.[1] === [2]) { console.log('Queue is empty'); }
Arrays use length to check how many elements they have. If length is 0, the queue is empty.
Fill all three blanks to create a function that inserts an element and keeps the queue sorted by priority.
function enqueue(queue, element) {
queue.push(element);
queue.sort((a, b) => a.[1] [2] b.[3]);
}The function sorts the queue by the priority property in ascending order using <.
