Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 理解雄辩的JS(练习2.3)_Javascript_If Statement - Fatal编程技术网

Javascript 理解雄辩的JS(练习2.3)

Javascript 理解雄辩的JS(练习2.3),javascript,if-statement,Javascript,If Statement,我正在用流利的Javascript编写第2章练习3,棋盘。我在理解一个部分时遇到了一个问题,希望有人能提供一些启示 我的代码: var size = 7; var board = ""; for(var i = 0; i <= size; i++){ for(var j = 0; j <= size; j++){ if((i + j) % 2 === 0){ board += " "; } else {

我正在用流利的Javascript编写第2章练习3,棋盘。我在理解一个部分时遇到了一个问题,希望有人能提供一些启示

我的代码:

var size = 7;
var board = "";
for(var i = 0; i <= size; i++){
    for(var j = 0; j <= size; j++){
        if((i + j) % 2 === 0){
            board += " ";
        } else {
            board += "#";
        }
    }
    board += "\n";
}
console.log(board);

我不明白为什么我需要把I和j加在一起。第一个包含i变量的“for”循环不是在构造行吗?有多少行?其中,包含j变量的第二个“for”循环正在每一行中创建内容?

它正在创建棋盘格图案,以便每一次迭代中每一行的间隔为1。如果不使用该行,它将创建条纹

目前为
i+j%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########
如果它只是
j%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########
如果它只是
i%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########

它正在创建一个棋盘格图案,这样在每次迭代中,每行都会被一个棋盘格隔开。如果不使用该行,它将创建条纹

目前为
i+j%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########
如果它只是
j%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########
如果它只是
i%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########

它正在创建一个棋盘格图案,这样在每次迭代中,每行都会被一个棋盘格隔开。如果不使用该行,它将创建条纹

目前为
i+j%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########
如果它只是
j%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########
如果它只是
i%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########

它正在创建一个棋盘格图案,这样在每次迭代中,每行都会被一个棋盘格隔开。如果不使用该行,它将创建条纹

目前为
i+j%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########
如果它只是
j%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########
如果它只是
i%2

  # # # #
 # # # # #
  # # # #
 # # # # #
  # # # #
  # # # #
  # # # #
  # # # #
.
 ########

 ########

 ########

添加i和j是为了创建棋盘格图案。如果总和为偶数,则打印白色正方形(“”);否则它是黑色的(“#”)


此外,您的条件语句包含三个等号,而不是所需的==。

i和j的添加是为了创建棋盘格模式。如果总和为偶数,则打印白色正方形(“”);否则它是黑色的(“#”)


此外,您的条件语句包含三个等号,而不是所需的==。

i和j的添加是为了创建棋盘格模式。如果总和为偶数,则打印白色正方形(“”);否则它是黑色的(“#”)


此外,您的条件语句包含三个等号,而不是所需的==。

i和j的添加是为了创建棋盘格模式。如果总和为偶数,则打印白色正方形(“”);否则它是黑色的(“#”)


另外,您的条件语句包含一个三等号,而不是所需的==。

我想我回答有点晚,但无论如何都会这么做

这是因为您希望创建一个类似这样的模式

"# # # #"
" # # # "
因此,您将添加用于从外部循环计数的变量“i”和用于计数的内部循环的变量“j”,以借助if/else语句来决定是否输出“#”或“”(空格)。也不是说你的第一个外循环将保持在0,直到你的内循环中的可值“j”等于“size”。这意味着第一次运行(i+j)的值为=到0+1、0+2、0+3、0+4、0+5、0+6、0+7。完成内循环后,外循环将添加一个“\n”并重复该过程,但这次用于计数的外循环变量从0变为1,用于计数的内循环变量重置为0,表达式(i+j)现在等于1+0、1+2、1+3等

var size = 7;
var board = "";
for(var i = 0; i <= size; i++){ /*This will remain 0 the first run until the inner loop is completed then it will add the "\n" at the end*/
for(var j = 0; j <= size; j++){/* the variable j here will keep adding 1 to itslef until it j is equal to size*/
    if((i + j) % 2 === 0){/* here it will add both i+j then divide it by 2 in order to see if is an even number or not. 
                            remember it will look something like the this first run 0+1, 0+2, 0+3 ect.*/
        board += " ";// here if the number is even it will output a (space) 
    } else {
        board += "#";// here if the number is not even it adds "#"
    } // finally this inner loop gets repeated until your vairable "j" is equal to "size" 
}
board += "\n";/* then the outer loop adds a new line and your vairable "i" 
              becomes 1 and this whole loop gets reapeated until "i" is equal to "size"*/
}
console.log(board);    
var size=7;
var board=“”;

对于(var i=0;i我认为我回答有点晚,但无论如何我都会这么做

这是因为您希望创建一个类似这样的模式

"# # # #"
" # # # "
因此,您将添加用于从外部循环计数的变量“i”和用于计数的内部循环的变量“j”,以借助if/else语句来决定输出“#”或“”(空格)。此外,在内部循环中的可值“j”等于之前,您的第一个外部循环将保持为0“大小”。意味着第一次运行(i+j)的值是=到0+1,0+2,0+3,0+4,0+5,0+6,0+7。一旦内循环完成,外循环添加一个“\n”并重复该过程,但这次用于计数的外循环VARABLE从0变为1,用于计数的内循环变量重置为0,表达式(i+j)现在等于1+0,1+2,1+3等

var size = 7;
var board = "";
for(var i = 0; i <= size; i++){ /*This will remain 0 the first run until the inner loop is completed then it will add the "\n" at the end*/
for(var j = 0; j <= size; j++){/* the variable j here will keep adding 1 to itslef until it j is equal to size*/
    if((i + j) % 2 === 0){/* here it will add both i+j then divide it by 2 in order to see if is an even number or not. 
                            remember it will look something like the this first run 0+1, 0+2, 0+3 ect.*/
        board += " ";// here if the number is even it will output a (space) 
    } else {
        board += "#";// here if the number is not even it adds "#"
    } // finally this inner loop gets repeated until your vairable "j" is equal to "size" 
}
board += "\n";/* then the outer loop adds a new line and your vairable "i" 
              becomes 1 and this whole loop gets reapeated until "i" is equal to "size"*/
}
console.log(board);    
var size=7;
var board=“”;

对于(var i=0;i我认为我回答有点晚,但无论如何我都会这么做

这是因为您希望创建一个类似这样的模式

"# # # #"
" # # # "
因此,您将添加用于从外部循环计数的变量“i”和用于计数的内部循环的变量“j”,以借助if/else语句来决定输出“#”或“”(空格)。此外,在内部循环中的可值“j”等于“i”之前,您的第一个外部循环将保持在0大小“。这意味着第一次运行(i+j)的值是=到0+1、0+2、0+3、0+4、0+5、0+6、0+7。一旦内循环完成,外循环添加一个“\n”并重复该过程,但这次用于计数的外循环VARABLE从0更改为1,用于计数的内循环变量重置为0,表达式(i+j)现在等于1+0,1+2,1+3等

var size = 7;
var board = "";
for(var i = 0; i <= size; i++){ /*This will remain 0 the first run until the inner loop is completed then it will add the "\n" at the end*/
for(var j = 0; j <= size; j++){/* the variable j here will keep adding 1 to itslef until it j is equal to size*/
    if((i + j) % 2 === 0){/* here it will add both i+j then divide it by 2 in order to see if is an even number or not. 
                            remember it will look something like the this first run 0+1, 0+2, 0+3 ect.*/
        board += " ";// here if the number is even it will output a (space) 
    } else {
        board += "#";// here if the number is not even it adds "#"
    } // finally this inner loop gets repeated until your vairable "j" is equal to "size" 
}
board += "\n";/* then the outer loop adds a new line and your vairable "i" 
              becomes 1 and this whole loop gets reapeated until "i" is equal to "size"*/
}
console.log(board);    
var size=7;
var board=“”;

对于(var i=0;i我认为我回答有点晚,但无论如何我都会这么做

这是因为您希望创建一个类似这样的模式

"# # # #"
" # # # "
因此,您将添加用于从外部循环计数的变量“i”和用于计数的内部循环的变量“j”,以借助if/else语句来决定输出“#”或“”(空格)。此外,在内部循环中的可值“j”等于“i”之前,您的第一个外部循环将保持在0这意味着第一次运行(i+j)的值是=到0+1,0+2,0+3,0+4,0+5,0+6,0+7。一旦