Q: How to write Javascript to search nodes - without getElementsByClassName I'm very new at recursion, and have been tasked with writing getElementsByClassName in JavaScript without libraries or the DOM API. 
There are two matching classes, one of which is in the body tag itself, the other is in a p tag.
The code I wrote isn't working, and there must be a better way to do this. Your insight would be greatly appreciated.
var elemByClass = function(className) {
    var result = [];
    var nodes = document.body; //<body> is a node w/className, it needs to check itself.
    var childNodes = document.body.childNodes; //then there's a <p>  w/className

    var goFetchClass = function(nodes) {
        for (var i = 0; i <= nodes; i++) { // check the parent
            if (nodes.classList == className) {
                result.push(i);
                console.log(result);
            }
            for (var j = 0; j <= childNodes; j++) { // check the children
                if (childNodes.classList == className) {
                    result.push(j);
                    console.log(result);
                }
                goFetchClass(nodes); // recursion for childNodes
            }
            goFetchClass(nodes); // recursion for nodes (body)
        }
        return result;
    };
};


A: Here are some reasons:


*

*goFetchClass needs an initial call after you've defined it - for example, you need a return goFetchClass(nodes) statement at the end of elemByClass function

*the line for (var i = 0; i <= nodes; i++) { will not enter the for loop - did you mean i <= nodes.length ?

*nodes.classList will return an array of classNames, so a direct equality such as nodes.classList == className will not work. A contains method is better. 

*Lastly, you may want to reconsider having 2 for loops for the parent and children. Why not have 1 for loop and then call goFetchClass on the children? such as, goFetchClass(nodes[i])? 
Hope this helps. 

A: There are some errors, mostly logical, in your code, here's what it should have looked like
var elemByClass = function(className) {
    var result  = [];
    var pattern = new RegExp("(^|\\s)" + className + "(\\s|$)");

    (function goFetchClass(nodes) {
        for (var i = 0; i < nodes.length; i++) {
            if ( pattern.test(nodes[i].className) ) {
                result.push(nodes[i]);
            }
            goFetchClass(nodes[i].children);
        }
    })([document.body]);

    return result;
};

Note the use of a regex instead of classList, as it makes no sense to use classList which is IE10+ to polyfill getElementsByClassName
Firstly, you'd start with the body, and check it's className property.
Then you'd get the children, not the childNodes as the latter includes text-nodes and comments, which can't have classes.
To recursively call the function, you'd pass the children in, and do the same with them, check for a class, get the children of the children, and call the function again, until there are no more children.
