Javascript 棋盘弦

Javascript 棋盘弦,javascript,for-loop,Javascript,For Loop,我有一个关于这个JS代码如何生成字符串中的棋盘的问题。我不确定if语句中的条件究竟是如何工作的 x+y%除以2是==到0是一种计算结果为真的条件。我有点困惑的是它是如何评估为真的?我可以多解释一下 var boardSize = 8; var boardString = ''; // loops to calc y and x axis hash placement for (var y = 0; y < boardSize; y++){ for (var x = 0; x <

我有一个关于这个JS代码如何生成字符串中的棋盘的问题。我不确定
if
语句中的条件究竟是如何工作的

x+y
%
除以
2
==
0
是一种计算结果为
真的条件。我有点困惑的是它是如何评估为真的?我可以多解释一下

var boardSize = 8;
var boardString = '';
// loops to calc y and x axis hash placement
for (var y = 0; y < boardSize; y++){
    for (var x = 0; x < boardSize; x++) {
        // x + y divided by 2 is === to 0
        if ((x + y) % 2 === 0){
            // true add hashes to string
            boardString += ' ';
        } else {
            boardString += '#';
        }
    }
    // newline for each set of hashes
    boardString += '\n';
}
console.log(boardString);
var boardSize=8;
var boardString='';
//循环到计算y轴和x轴散列位置
对于(变量y=0;y
除了一条误导性的评论外,代码中还有几处输入错误:

boardSize+=''应该是
boardString+=''

(var x=0;y
应该是
(var x=0;x


如果你用每个方格中
x
y
的值来考虑棋盘(使用符号
y,x

你想要的是黑白相间的方块。从上面可以看出,如果
x
y
之和为偶数,则正方形为白色;如果为奇数,则正方形为黑色

a%2===0
true
如果
a
为偶数(余数运算符
%
就是这样工作的)


因此,
((x+y)%2===0)
意味着一个白色的正方形。

除了一个误导性的注释外,代码中还有几个拼写错误:

boardSize+=''应该是
boardString+=''

(var x=0;y
应该是
(var x=0;x


如果你用每个方格中
x
y
的值来考虑棋盘(使用符号
y,x

你想要的是黑白相间的方块。从上面可以看出,如果
x
y
之和为偶数,则正方形为白色;如果为奇数,则正方形为黑色

a%2===0
true
如果
a
为偶数(余数运算符
%
就是这样工作的)


因此,
((x+y)%2==0)
表示白色正方形。

例如,作为任何坐标

          n-1,m
n,m-1     n,m     n,m+1
          n+1,m
计算总数

          n+m-1
n+m-1     n+m     n+m+1
          n+m+1
然后我们做
%2

      1                         0
1     0     1      or     0     1     0
      1                         0 
扩展它

1 0 1 0 1 0 1               0 1 0 1 0 1 0
0 1 0 1 0 1 0               1 0 1 0 1 0 1
1 0 1 0 1 0 1       or      0 1 0 1 0 1 0     
0 1 0 1 0 1 0               1 0 1 0 1 0 1
1 0 1 0 1 0 1               0 1 0 1 0 1 0

事实上,它们是一样的,你有一个棋盘,比如任何坐标

          n-1,m
n,m-1     n,m     n,m+1
          n+1,m
计算总数

          n+m-1
n+m-1     n+m     n+m+1
          n+m+1
然后我们做
%2

      1                         0
1     0     1      or     0     1     0
      1                         0 
扩展它

1 0 1 0 1 0 1               0 1 0 1 0 1 0
0 1 0 1 0 1 0               1 0 1 0 1 0 1
1 0 1 0 1 0 1       or      0 1 0 1 0 1 0     
0 1 0 1 0 1 0               1 0 1 0 1 0 1
1 0 1 0 1 0 1               0 1 0 1 0 1 0

实际上,它们是相同的,并且你有一个棋盘,因此
if
语句被认为是一个白色的正方形,因为
x
y
的总和是偶数

var boardSize = 8;
var boardString = '';
// loops to calc y and x axis hash placement
// think of x and y as white and black squares
// white square = x and y are even. black square = odd.
for (var y = 0; y < boardSize; y++){
    for (var x = 0; x < boardSize; x++) {
        // x + y is true if the remainder of the division is even
        // even = a white square
        if ((x + y) % 2 === 0){
            // true add hashes to string
            boardString += ' ';
        } else {
            boardString += '#';
        }
    }
    // newline for each set of hashes
    boardString += '\n';
}
console.log(boardString);
var boardSize=8;
var boardString='';
//循环到计算y轴和x轴散列位置
//将x和y视为白色和黑色正方形
//白色正方形=x和y是偶数。黑色正方形=奇数。
对于(变量y=0;y
因此
if
语句被认为是一个白色的正方形,因为
x
y
的总和是偶数

var boardSize = 8;
var boardString = '';
// loops to calc y and x axis hash placement
// think of x and y as white and black squares
// white square = x and y are even. black square = odd.
for (var y = 0; y < boardSize; y++){
    for (var x = 0; x < boardSize; x++) {
        // x + y is true if the remainder of the division is even
        // even = a white square
        if ((x + y) % 2 === 0){
            // true add hashes to string
            boardString += ' ';
        } else {
            boardString += '#';
        }
    }
    // newline for each set of hashes
    boardString += '\n';
}
console.log(boardString);
var boardSize=8;
var boardString='';
//循环到计算y轴和x轴散列位置
//将x和y视为白色和黑色正方形
//白色正方形=x和y是偶数。黑色正方形=奇数。
对于(变量y=0;y
考虑循环的版本:

for(变量y=0;yboardString+='#'[(x+y)%2]
考虑循环的版本:

for(变量y=0;yboardString+='#'[(x+y)%2]
该注释具有误导性,
(x+y)%2===0
表示“如果x+y除以2的剩余部分为零”。该注释具有误导性,
(x+y)%2==0
表示“如果x+y除以2的剩余部分为零”。此外,还有一个致命的输入错误:for(var x=0;yx
y
的总和为偶数,如
4
,则在电路板上放置一个白色正方形。我发现有点困惑的是,我需要检查它是否是
==
0
===0
正在检查左侧的位是否为偶数。为什么不使用调试器在每个步骤上检查
(x+y)%2的值?那么你就会明白了。另外,还有一个致命的打字错误:for(var x=0;yx
y
的总和甚至是
4
,那么一个白色的方块就会被放在棋盘上。我发现有点困惑的是,我需要检查它是否是
==
0