Javascript棋盘打印

Javascript Chessboard Print

本文关键字:打印 Javascript      更新时间:2023-09-26

我正在学习如何用Javascript编写代码,我试图解决的一个练习是如何将棋盘打印到控制台,使其看起来像这样:

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

我可以使用两个for循环来完成这项工作,但我想只使用一个for循环,以提高效率。这就是我目前所拥有的:

var x = "#";
for(var i = 1; i < 65; i++) {

 if((i%9 == 0)) {
    x = "'n" + x.charAt(i-1);
  }
  else if (x.charAt(i-1) == "#") {
        x = x + " "
  }
  else {
    x = x + "#";
  }
}
console.log(x);

这只是发布了一个"#",我不知道为什么。感谢您的帮助!

哦,代码高尔夫!

var x = new Array(33).join('#').split('').map(function(e,i) {
    return (i % 4 == 0 ? (i === 0 ? '' : ''n') + (i % 8 ? ' ' : '') : ' ') + e;
}).join('');
document.body.innerHTML = '<pre>' + x + '</pre>'; // for code snippet stuff !

要修复原始函数,您必须实际添加到字符串中,通过使用''n' + x.charAt(i-1);,您将获得一个换行符和一个字符,正如charAt所做的那样,它将在该索引处获得一个字符。因此,字符串永远不会超过一个#

var x = "#";
for (var i = 1; i < 65; i++) {
    if (i % 9 == 0) {
        x += "'n";
    } else if (x[x.length-1] == "#") {
        x += " "
    } else {
        x += "#";
    }
}

这解决了这个问题,但它仍然不会打乱模式,您需要为

添加额外的逻辑

想想当你调用这行时会发生什么:

x = "'n" + x.charAt(i-1);

x添加到一个换行符中,并添加一个SINGLE字符。你明白为什么你的绳子现在不长了吗?

使用:

        x = x + "'n" + x.charAt(i-1);

而不是

x ="'n" + x.charAt(i-1);

为了得到确切的模式,您必须在代码中添加一些额外的逻辑。

请参阅我的解决方案,它使用循环中循环,非常简单。此外,你可以放置任何宽度长度的棋盘:

// define variable for grid size 
const size = 8;
// outer loop handles the quantity of lines
for (let i = 1; i <= size; i++) {
    // define sting variable for line
    let line = "";
    // inner loop handles what characters will be in line
    for (let j = 1; j <= size; j++) {
        // check what character to start with: space or #
        // to take into account both loops check if sum of i and j is even or not
        (j + i) % 2 === 0 ? line += "#" : line += " ";
    }
    // print the line and go to next line using "'n" symbol
    console.log(line + "'n");
}

//create a variable and assign a space to it
let result = " ";
//create a nested for loop for the 8 X 8 tiles.
 for (let x = 0; x < 8; x++){
    for (let z = 0; z < 8; z++){
//Replace each even character by space
      if (0 == (z + x) % 2){
        result += " ";
      } else if (0 != (z + x) % 2){
//otherwise replace each odd character by a "#"
        result += "#";
      }   
    }    
//to keep each line blocks of 8, use 'n 
    result += "'n";
} 
//to print in a straight line, console.log outside the forloop 
console.log(result);

 let size = 8, result= '';
    for(let i=0; i < size; i++) { 
      for(let j=0; j < size; j++) {
          if(i !== 0 && j === 0 ) {
          result += ''n';
          }
          if( (i%2 === 0 && j%2 === 0) || (i%2 ===1 && j%2 === 1)) {
           result += ' ';
          } else if( (i%2 !== 0 && j%2 === 0) || (i%2 === 0 && j%2 !== 0)) {
            result += '#';
          }
      }
    }
    
    console.log(result);

在这里,我刚刚学会:)。

我是这样做的:

let chess = "";
for (i = 1; i < 9; i += 1) {
  for (j = 1; j < 9; j += 1) {
    if (i % 2 == 0) {
      if (j % 2 == 0) {
        chess = chess + "#";
      } else {
        chess = chess + " ";
      }
    } else if (i % 2 !== 0) {
      if (j % 2 == 0) {
        chess = chess + " ";
      } else {
        chess = chess + "#";
      }
    }
  }
  chess = chess + "'n";
}
console.log(chess);