Home » Javascript » Two identical functions one works one does not

Two identical functions one works one does not

Posted by: admin October 29, 2018 Leave a comment

Questions:

I created two functions, GOODself and BADself, called in succession to debug a problem I have in a larger script illustrated here. I can’t figure out why BADself stalls at the line aryVarAbi[0] = 5;.

If I call it first it still happens. According to the console in Firefox aryVarAbi is not defined.

GAB = GOODself(0, 4, 1);
GAB = BADself(0, 4, 1);

function GOODself(GABin, nCols, nRows) {
    var aCol = 0;
    var aryVarABi = [1,1,1,1];
    for (aCol=0; aCol < nCols - 1; aCol++) {
        alert("GOOD1 " + aryVarABi[aCol]);
        aryVarABi[0]= 5;
        alert("GOOD2 " + aryVarABi[aCol]);
    } // for aCol
    return (aryVarABi[0]);
} // GOODself

function BADself(GABin, nCols, nRows) {
    var aCol = 0;
    var aryVarABi = [1,1,1,1];
    for (aCol=0; aCol < nCols - 1; aCol++) {
        alert("BAD1 " + aryVarABi[aCol]);
        aryVarAbi[0] = 5;
        alert("BAD2 " + aryVarABi[aCol]);
    } // for aCol
    return (aryVarABi[0]);
} // BADself
Answers: