Exercise 11: Prevent Default and Stop Propagation

Problem Statement

Function `handleEvent(e, handler)` banao jo event ko handle kare with preventDefault aur stopPropagation. Example: handleEvent(e, () => console.log("clicked"))

Sample Output:

handleEvent(e, () => console.log("clicked"))
// Prevents default, stops propagation, then calls handler

Solution

const handleEvent = (e, handler) => {
  e.preventDefault();
  e.stopPropagation();
  handler(e);
};

Explanation

Overall Goal:

  • Event handling with default prevention.

Real world:

  • Form submission: prevent default.
  • Event handling: stop propagation.