Javascript棋盘打印

7
我正在学习如何用 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);

这只发布了一个“#”符号,我不确定原因。任何帮助都将不胜感激!
7个回答

9

哦哦哦,代码高尔夫!

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 += "#";
    }
}

那样做解决了问题,但仍无法交错模式,您需要额外的逻辑来实现。

很酷,但并没有真正回答他得到输出的原因... - Dair

5

试想一下当您拨打此线路时会发生什么:

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

你可以通过添加一个字符,将x转换为换行符。你现在明白为什么你的字符串不再很长了吗?

5

使用:

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

而不是

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

要获取准确的模式,您需要在代码中添加一些额外的逻辑。

@dsfq 要实现这个,我认为需要添加一些额外的代码。 - Suchit kumar
是的,你说得对。从技术上讲,你回答了“为什么”的问题,而OP并没有问“如何”。很公平 :) - dfsq

0
请看我的解决方案,它使用了循环嵌套,非常简单。 此外,您可以放置任何宽度和长度的棋盘:
// 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");
}

0

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


0

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

在这里,我刚刚学习了:)


-1
我是这样做的:
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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接