TypeError: Cannot read properties of undefined (reading 'length')

suyeon·2022년 1월 18일
0

TypeError: Cannot read properties of undefined (reading 'length')
at Object.

위와 같은 에러가 발생했다.

for (var i=1; i<= input[0]; i++) {
    var score = 1;
    var total = 0;
    for (var j=0; j<input[i].length; j++) {
        if (input[i][j] === 'O') {
            total += score;
            score ++;
        } else {
            score = 1;
        }
    }
    console.log(total);
}

두번째 for문에서 j++를 i++로 잘못 적어 발생한 것이었다.

0개의 댓글