2 回答

TA貢獻1786條經驗 獲得超13個贊
如果所有單元格都是“空的”,則讓所有單元格都包含一個空格字符可能更有意義??纯催@里:
var Cell_1 = "a";
var Cell_2 = " ";
var Cell_3 = " ";
var Cell_4 = " ";
var Cell_5 = " ";
var Cell_6 = " ";
var Cell_7 = " ";
var Cell_8 = " ";
var Cell_9 = " ";
console.log(
Cell_1 + "|" + Cell_2 + "|" + Cell_3 + "\n" +
Cell_5 + "|" + Cell_6 + "|" + Cell_6 + "\n" +
Cell_7 + "|" + Cell_8 + "|" + Cell_9 + "\n" +
)
這樣你所有的變量都是相同的寬度 - 一個字符。
為了將來參考,這里有一些代碼可能看起來更好一些:
// This is called a 2d array: essentially an array containing other arrays.
// Its good for storing grids or tables of information.
var cells = [
['a', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']
]
// This uses Array.reduce() to generate the string.
// Google it once you feel more confident :)
console.log(
cells.reduce(
(totalString, currentRow) => totalString + currentRow.join('|') + '\n',
''
)
)

TA貢獻1824條經驗 獲得超5個贊
這個問題不是很清楚,但我假設你想保持網格對齊,網格有多個單元格,可以包含一個字符,也可以不包含一個字符。
問題是空單元格被初始化為""
大小為 0 的 (empty string),但是當設置字符時,大小將為 1,因此它會將所有以下單元格移動 1。
一個簡單的解決方案是對空單元格使用" "
(space) 而不是""
. 因此,單元格的大小將始終為 1,并且不會移動整個網格。
添加回答
舉報