I have a function to get text inside tags. One version works the other does not. The only difference is that in one I use 'elem' all the time, and in the other version I assign 'elem' to 'elems'. I'm really dumbfounded.

Code:
/* This version does NOT work becuase I am using 'elems' (note the s). */
function getText1(elem) {
    var str = '';

    elems = elem.childNodes || elem;
    for (var i = 0; i < elems['length']; i++) {
        //str += elem[i].nodeType != 1 ? elem[i].nodeValue : getText(elem[i].childNodes);
        if (elems[i].nodeType != 1)
            str += elems[i].nodeValue;
        else
            str += getText(elems[i].childNodes);
    }
    return str;
}
Code:
/* This one works. Not using 'elems'. Using 'elem' all the time. */
function getText2(elem) {
    var str = '';

    elem = elem.childNodes || elem;
    for (var i = 0; i < elem['length']; i++) {
        //str += elem[i].nodeType != 1 ? elem[i].nodeValue : getText(elem[i].childNodes);
        if (elem[i].nodeType != 1)
            str += elem[i].nodeValue;
        else
            str += getText(elem[i].childNodes);
    }
    return str;
}

Sample HTML:
Code:
<p>Hello, <strong>how</strong> are you doning?</p>
Results
Code:
var v = document.getElementsByTagName('p')[0];
console.log(getText1(v)); // prints 'Hello, how'
console.log(getText2(v)); // prints 'Hello, how are you doing?'.
What is going on? Why getText1() partially works, while getText2() works as expected?