0

when I run the code below I get an error in the browser console which reads: Uncaught TypeError: Cannot read property '0' of undefined

The code does print what I intended though I am curious to the nature of the error. I have pasted the output at the bottom of this post.

var chessBoard = [];
for(let i = 0; i < 8; i++) {
   chessBoard[i] = [];
   for(let j = 0; j < 8; j++) {
   chessBoard[i][j] = (i + j)% 2 === 0 ? 'Black' : 'White';
}} // populate a 2 dimensional array with colors representing a chess board.

for(let i = 7; i => 0; i--) {
    let str_horizontal = '';
    for (let j = 0; j < 8; j++) {
        str_horizontal += chessBoard[i][j];
    }
    console.log(str_horizontal);
}

WhiteBlackWhiteBlackWhiteBlackWhiteBlack VM116:13 BlackWhiteBlackWhiteBlackWhiteBlackWhite VM116:13 WhiteBlackWhiteBlackWhiteBlackWhiteBlack VM116:13 BlackWhiteBlackWhiteBlackWhiteBlackWhite VM116:13 WhiteBlackWhiteBlackWhiteBlackWhiteBlack VM116:13 BlackWhiteBlackWhiteBlackWhiteBlackWhite VM116:13 WhiteBlackWhiteBlackWhiteBlackWhiteBlack VM116:13 BlackWhiteBlackWhiteBlackWhiteBlackWhite VM116:11

Uncaught TypeError: Cannot read property '0' of undefined at :11:40

I have been struggling with this a while now and have made little progress. I cannot see the problem. I appreciate any help, Thanks.

3
  • 3
    => is not what you think it is, be more careful when writing operators. Commented Mar 14, 2018 at 15:06
  • In the words of Alan Partridge... ahhh hah!! Commented Mar 14, 2018 at 15:09
  • Thanks very much Commented Mar 14, 2018 at 15:09

1 Answer 1

3

In your for loop you're using an arrow function , instead of => put >=

var chessBoard = [];
for(let i = 0; i < 8; i++) {
   chessBoard[i] = [];
   for(let j = 0; j < 8; j++) {
   chessBoard[i][j] = (i + j)% 2 === 0 ? 'Black' : 'White';
}} // populate a 2 dimensional array with colors representing a chess board.

for(let i = 7; i >= 0; i--) {
    let str_horizontal = '';
    for (let j = 0; j < 8; j++) {
        str_horizontal += chessBoard[i][j];
    }
    console.log(str_horizontal);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.