I am trying to write a getElementByClassName from scratch, but I am not sure when to return the recursion. That's what I have came up with:
const getElementsByClassName = (nameOfClass, parent) => {
const result = []
for(let el of parent) {
// console.log(el.children)
if(el.firstElementChild != null) {
// do it again on node deeper
getElementsByClassName(nameOfClass, el.children)
}
if(el.className === nameOfClass) {
result.push(el)
}
}
// when do I want to return result?
console.log(result)
return result
};
The problem is that I have one array per child node, instead of everything in the same array result. How can I solve this?