← All Exercises

Resolve Delegated Click Actions from an Event Path

basicjavascript~15min

Background

Delegated listeners rely on closest(); walking a path models that lookup and shows why inner elements need no handlers of their own.

Problem

Write resolveAction(path) that walks a simulated event path outward, finds the nearest node with a data-action attribute, and returns its action and item id.

Input

A JSON string with a path array of node descriptors.

Output

A JSON string with action and id keys, or null when no match exists.

Sample tests

Sample 1

Input: {"path":[{"tag":"span","attrs":{}},{"tag":"button","attrs":{"data-action":"delete","data-id":"42"}},{"tag":"li","attrs":{}}]}
Expected: {"action":"delete","id":"42"}

Sample 2

Input: {"path":[{"tag":"path","attrs":{}},{"tag":"svg","attrs":{"data-action":"save"}}]}
Expected: {"action":"save","id":null}

Sample 3

Input: {"path":[{"tag":"li","attrs":{}},{"tag":"ul","attrs":{}}]}
Expected: null

Your solution

Learning objective

Implement closest-ancestor action lookup for delegated events and normalize missing dataset values.