Codetive
Sign In
Coding challenges as of now are only available on desktop
On mobile you can try our quiz
Quiz
We are working hard to bring coding challenges with editor on mobile 💚

Event queue

Objective: Implement an event queue system that manages user actions (like liking posts) in sequence and handles cancelable events (e.g., like + unlike = no action).

Instructions:

  1. Create a function called createEventQueue that manages an event queue.
  2. The event queue should support adding and canceling actions (e.g., "like post" followed by "unlike post" results in no event).
  3. Ensure that events are processed in the order they were added, but allow new events to be triggered while previous events are still being processed.

Example:

const eventQueue = createEventQueue();
 
eventQueue.add('like', () => console.log('Liked post 1'));
eventQueue.add('unlike', () => console.log('Unliked post 1'));
eventQueue.add('like', () => console.log('Liked post 2'));
 
// Output:
// Liked post 2
 
// In this case, the "like" and "unlike" actions for post 1 cancel each other,
// so only the "Liked post 2" event is processed.

Requirements:

  • Create a queue system that stores actions and processes them in sequence.
  • Ensure that cancelable actions (like/unlike) cancel each other when triggered for the same post.
  • Provide a way to trigger new events while the queue is still being processed.

Hints:

  1. Use an array to store the events in the queue.
  2. Keep track of actions (like/unlike) to determine if they cancel each other out before executing them.
Browser
Console
Tests
Soon