function chessBoard (size) {
result = "";
for (var s = 1; s <= size; s++ ){
for (var c = 1; c <= size; c++){
if ((s + c) % 2 == 0) {
result = result + " ";
}
else {
result = result + "#";
}
}
result = result + "\n";
}
console.log(result);
}
chessBoard(8);
结果:
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
为什么&#34;棋盘的第一行&#34;结果在此代码中未对齐?帮助我了解需要改变的内容。它应该看起来像:
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
答案 0 :(得分:0)
来自@barmar:Chrome会自动在其记录的字符串前放置一个空格