Hi. 🙂 Let's name the function as crawlElements . /** * Recursively collects all nested element nodes from * a given DOM node. * * @param {Node} node - The root DOM node from which * to begin the search. * @returns {Element[]} An array of all descendant * element nodes found beneath * the input node. * * @example * const root = document.getElementById("container"); * const allElements = crawlElements(root); * console.log(allElements); // Array of nested div, span, etc. */ function crawlElements(node) { let elements = []; const isElement = node && node instanceof Element; const hasChildren = isElement && node.hasChildNodes(); if (!hasChildren) return elements; node.childNodes.forEach((child) => { if (child.nodeType === Node.ELEMENT_NODE) { elements.push(child); elements = [...elements, ...crawlElements(child)]; ...