Need to create a chessboard with the following rule set:
Write a program that creates a string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a "#" character. The characters should form a chessboard.
Passing this string to console.log should show something like this:
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
Here is the code i wrote for it:
var chessBoardSize = 8;
var chessBoard = " ";
var lineBreakCount = 0;
while (chessBoard.length <= chessBoardSize * chessBoardSize) {
lineBreakCount++;
if (lineBreakCount === chessBoardSize) {
chessBoard += "\n";
lineBreakCount = 0;
}
if (chessBoard.slice(-1) === " ") {
chessBoard = chessBoard + "#";
} else if (chessBoard.slice(-1) === "#") {
chessBoard = chessBoard + " ";
}
}
console.log(chessBoard);
I'm having trouble to get the string on a new line once 8 characters have been used on that line. This is the part of the program i'm trying to figure out:
lineBreakCount++;
if (lineBreakCount === chessBoardSize) {
chessBoard += "\n";
lineBreakCount = 0;
}
When i run the code, i get back this:
# # # #
But, it should be this:
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
Aucun commentaire:
Enregistrer un commentaire