Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions web/src/pages/challenges/dom.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,19 @@ function cloneDOMTree(root) {

```js copy
/**
* @param {HTMLElement | null} tree
* @return {number}
* Counts all child elements in a DOM tree.
*
* @param {HTMLElement | null} root - The root element from which to start counting.
* @return {number} - The total number of child elements.
*/
function countElements(root) {
if (!root) return 0;

let count = 1; // Count this element
let count = 1;

if (root.hasChildNodes()) {
for (let child of root.children) {
count += countElements(child);
}
for (let child of root.children) {
count += countElements(child);
}

return count;
}
```
Expand Down