I'm new to Javascript and trying to put together a checkerboard for a course. I have to set up the board with the black and red checkers (i.e. the first three rows for red, the last three for black, every other tile has a checker), and I'm a bit stuck. I tried the code below, which just results in each value in the array for var checkers
being printed as null
, followed by four 'B's
. I imagine I need to somehow incorporate the multidimensionality of the array into my for loop, but I have no idea how to do that. Any guidance or help is much appreciated!
var checkerboard = [[null,null,null,null,null,null,null,null],
[null,null,null,null,null,null,null,null],
[null,null,null,null,null,null,null,null],
[null,null,null,null,null,null,null,null],
[null,null,null,null,null,null,null,null],
[null,null,null,null,null,null,null,null],
[null,null,null,null,null,null,null,null],
[null,null,null,null,null,null,null,null]];
function setUpRed(square) {
return (square = 'R');
}
for (var i = 0; i < checkerboard [3][7]; i += 2 ) {
checkerboard.push(setUpRed(checkerboard[i]));
}
function setUpBlack(square) {
return (square = 'B');
}
for (var i = (checkerboard.length - 1); i > checkerboard [6][0]; i -= 2) {
checkerboard.push(setUpBlack(checkerboard[i]));
}
console.log(checkerboard);