gpt4 book ai didi

javascript - Eloquent JS : Chessboard – why is first row misaligned?

转载 作者:行者123 更新时间:2023-11-28 07:44:23 29 4
gpt4 key购买 nike

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);

结果:

  # # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #

为什么这段代码中“棋盘”结果的第一行没有对齐?帮助我了解需要更改的内容。它应该看起来像:

 # # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #

最佳答案

来自 @barmar:Chrome 会自动在其记录的字符串前放置一个空格

关于javascript - Eloquent JS : Chessboard – why is first row misaligned?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27613933/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com