Frontend101 logoFrontend101
← Problems
#9MediumJavaScript functions

9. getElementsByStyle

JavaScriptWeb APIsHTML

Implement getElementsByStyle(root, property, value) to find all descendant nodes whose style[property] matches value.

Example 1:
getElementsByStyle(tree, "color", "red");
solution.js
Finds descendants with matching inline style property
(() => {
          const tree = {
            id: "root",
            style: { color: "red" },
            children: [
              { id: "a", style: { color: "blue" }, children: [{ id: "b", style: { color: "red" }, children: [] }] }
            ]
          };
          expect(getElementsByStyle(tree, "color", "red").map((n) => n.id)).toEqual(["b"]);
        })()