use JS to LeetCode, to question 6 ZigZag Conversion report an error TypeError: Cannot read property "push" of undefined
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
var convert = function(s, numRows) {
var rowArray = new Array(numRows),
dir = true,
n = 1,
i = 0,
temp = 0,
rowArrayCount = 0,
ans = "",
len = s.length;
for (i = 0; i < numRows; iPP) {
rowArray[i] = new Array();
}
while (i<len) {
console.log(rowArray);
console.log(rowArray instanceof Array);
console.log(rowArray[rowArrayCount] instanceof Array);
while(dir) {
rowArray[rowArrayCount].push(s.charAt(i));
rowArrayCountPP;
iPP;
if (i === numRows*n -1) {
dir = false;
nPP;
rowArrayCount = numRows-1;
}
}
while(!dir) {
rowArray[rowArrayCount].push(s.charAt(i));
rowArrayCount--;
iPP;
if (i === numRows*n -1) {
dir = true;
nPP;
rowArrayCount = 0;
}
}
}
for (i = 0; i < numRows; iPP) {
temp = rowArray[i].join("");
ans += temp;
}
return ans;
};
where
console.log(rowArray);
console.log(rowArray instanceof Array);
console.log(rowArray[rowArrayCount] instanceof Array);
output is:
Why is rowArray [rowArrayCount] an array but not the push method?