Skip to the code
cf.
+10 XP

#706 Path of a Click

easy

When an event fires, event.composedPath() lists the target first, then its parent, then each ancestor up to the top.

The page is given as parentOf, an object that maps each element's id to its parent's id; the root maps to null. Write eventPath(parentOf, targetId) that returns the ids from targetId up to the root, in that order.

Example 1

Input: parentOf = {"root":null,"form":"root","field":"form","input":"field","submit":"form","aside":"root"}, targetId = "input"
Output: ["input","field","form","root"]

Climb from input through its parents until the root, whose parent is null.

Example 2

Input: parentOf = {"root":null,"form":"root","field":"form","input":"field","submit":"form","aside":"root"}, targetId = "root"
Output: ["root"]

The root has no parent, so the path is just the root.

Constraints

  • parentOf has 1 to 10^4 entries and describes a single tree.
  • targetId is a key of parentOf.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).