I need some help write the logic for finding the number of descendants of an element.
html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<main>
<ul>
<li><button>item</button></li>
</ul>
<p>Hello, world</p>
</main>
<script src="index.js"></script>
</body>
</html>
index.js :
function countDescendants(element) {}
// Test code
const result = countDescendants(document.querySelector("main"));
if (result !== 4) {
console.error(`fail: ${result}`);
} else {
console.log("Pass!");
}
Can some help me to write the countDescendants function.We cant use built in function. should write our own logic. For this particular example it should return the result 4.