简体   繁体   English

为什么这些雄辩的Javas Chessboard解决方案之一比另一个更好

[英]Why is one of these Eloquent Javascript Chessboard solutions better than the other

I took on the Chessboard puzzle from the book Eloquent Javascript and devised the following solution without looking at hints 我从《 Eloquent Javascript》一书中研究了Chessboard难题,并设计了以下解决方案,而没有注意任何提示

var newline = "\n";
var counter = .5;
var string1 = " # # # #";
var string2 = "# # # # ";
while (counter <= 3.5){
  console.log(string1 + newline);
  console.log(string2 + newline);
  counter++
}

I originally had too many lines written out so simply changed my counter to 'half steps'. 我最初写了太多行,因此只需将计数器更改为“半步”即可。 Looking up how others accomplished this, I found this solution. 通过查找其他人如何完成此任务,我找到了此解决方案。

    var size = 8;
for (var i = 0; i < size; i++) {
  var line = "";
  for (var j = 0; j < size; j++) {
    var total = i + j;
    if (total % 2 == 0) { 
      line += '#';
    } else {
      line += ' ';
    }
  }
  console.log(line);
}

Can you explain why one version may be better than the other? 您能解释一下为什么一个版本可能比另一个版本更好吗? Also, a plain-english explanation of the second would be helpful. 同样,对第二种进行简单的英语解释将很有帮助。 I talked myself into a headache trying to comment it plainly. 我让自己陷入头痛,试图对此发表清楚的评论。

Plain english explanation of second one - the var size = 8 will be the size of the board. 第二个简单的英语解释var size = 8将是电路板的大小。 The first for loop declares the var line and eventually logs it to console. 第一个for循环声明var line并最终将其记录到控制台。 It will log to console for every line, or row, if you prefer. 如果愿意,它将登录到控制台的每一行或每一行。

The second for loop will actually build the line, or row, adding to var line for each column in the row. 第二个for循环实际上将构建行或行,为var line中的每一列添加到var line行。 Instead of having to declare two strings like you did in the first version, it knows how each row should end up looking based on some variables and a rule. 它无需像在第一个版本中那样声明两个字符串,而是知道每行最终如何基于一些变量和规则来查找。 The rule is if total is divisible by 2, then add a "#", and if not, then add a " ". 规则是,如果total被2整除,则添加“#”,否则,添加“”。 total is calculated by adding i and j . total是通过将ij相加得出的。

So in the first row i is always 0, and j will be 0, then 1, then 2, etc...so total be divisible by 2, then not divisible by 2, then divisible by 2, etc...then in the second row i will be equal to 1, and j again will be 0, then 1, then 2, etc..so now total will first not de divisible by 2, then divisible by 2, then not, etc...for the third row, i will be 2, which will basically act as i being 0 since both 0 and 2 leave no remainder when divided by 2. This is how the second for loop accomplishes the same thing as your string1 and string2 . 因此在第一行中, i始终为0, j始终为0,然后为1,然后为2,以此类推...因此total可以被2整除,然后不能被2整除,然后被2整除,以此类推...然后第二行i等于1, j再次为0,然后为1,然后为2,依此类推。因此, total将首先不被2整除,然后被2除数,然后不以此类推...对于第三行, i将为2,因为当0和2除以2时都没有余数,所以i将基本上作为i为0。这就是第二个for循环如何完成与string1string2相同的操作。 I'm sorry this is a bit wordy, hope it makes sense...I'll put some comments in the actual code below. 抱歉,这有点罗word,希望它有意义...我将在下面的实际代码中添加一些注释。

// sets size of board, since the first loop will run for this amount of
// times, and log a row to console each time, and the second loop will add   
// this many characters to the string to be logged.
var size = 8;

// first loop, which will run a number of times equal to the size
// variable, and log a row to console each time
for (var i = 0; i < size; i++) {

  // declares the string to be logged as the row
  var line = "";

  // this loop will run a number of times equal to the size
  // variable, and add a character to the line variable each time,
  // either a "#" or a " "
  for (var j = 0; j < size; j++) {

    // the total variable will be used to determine if the character added
    // to the line is a "#" or a " "
    // basically, any time i is 0 or divisible by 2, the row will
    // begin with a "#" and alternate from there as j changes, 
    // if i is not divisible by 2, the row will begin with a " " 
    // and alternate from there
    var total = i + j;

    // this if else statement actually uses total to added either "#" or " "
    if (total % 2 == 0) { 
      line += '#';
    } else {
      line += ' ';
    }
  }

  // this is outside of the second loop, and now the line variable is done
  // being added to, and the line is logged
  console.log(line);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM