gpt4 book ai didi

javascript - Javascript数组中的魔术逗号

转载 作者:行者123 更新时间:2023-11-29 15:13:38 25 4
gpt4 key购买 nike

我正在编写一个数独验证程序作为练习题。我在一行中导入一个 81 位数字板,由一个空格作为字符串分隔。当我在第 4 行打印字符串时,输入中没有逗号分隔数字。在第 14 行,我使用 split 方法将其拆分为一个长度应为 81 个索引的数组。当我通过在第 20 行打印数组来测试它时,数字之间的索引已用逗号填充。我不知道这些神奇的逗号是从哪里来的,它一定是拆分方法的结果,因为我的输入不包含任何逗号。谁能解释发生了什么?这是 JS 脚本:

function import1() {
var board = prompt("Please enter a board");
if (board != null) {
document.getElementById("demo").innerHTML = "board entered: " + board + "\n";
}
else {
document.getElementById("demo").innerHTML = "Invalid entry\n";
}

parseMe(board);
}

function parseMe(board) {

var boardArray = String(board.split(" "));

var toPrint = "";
for (var j = 0; j < boardArray.length; j++) {
toPrint += boardArray[j];
}
document.getElementById("test").innerHTML = "test" + toPrint;


var board2D = [
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"]
];

var counter = 0;
for (var i = 0; i < board2D.length; i++) {
for (var j = 0; j < board2D.length; j++) {
board2D[i][j] = boardArray[counter];
counter++;
}
}
if (counter != 81) {

document.getElementById("error").innerHTML = "Invalid entry, should be 81 total digits\n";
}
printBoard(board2D);
}

function printBoard(board) {
var toPrint = "";

for (var i = 0; i < board.length; i++) {
for (var j = 0; j < board.length; j++) {
toPrint += board[i][j] + " ";
}
toPrint += "\n";
}
document.getElementById("printBoard").innerHTML = toPrint;
}

如果有人想看,我可以提供我写信的网页,但我认为这不重要。

输出结果如下:

输入板:5 3 4 6 7 8 9 1 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9

测试5,3,4,6,7,8,9,1,2,6,7,2,1,9,5,3,4,8,1,9,8,3,4,2 ,5,6,7,8,5,9,7,6,1,4,2,3,4,2,6,8,5,3,7,9,1,7,1,3,9 ,2,4,8,5,6,9,6,1,5,3,7,2,8,4,2,8,7,4,1,9,6,3,5,3,4 ,5,2,8,6,1,7,9

最佳答案

board.split("")(数组)转换为字符串时,将插入逗号。请参阅文档 here ),以及以下示例:

var test = ['1', '2', '3'];

console.log(test[0], test[1]);

var testAsString = String(test);

console.log(testAsString[0], testAsString[1]);

您不需要将其从数组中转换回来,只需正常访问元素即可。

function import1() {
var board = prompt("Please enter a board");
if (board != null) {
document.getElementById("demo").innerHTML = "board entered: " + board + "\n";
}
else {
document.getElementById("demo").innerHTML = "Invalid entry\n";
}

parseMe(board);
}

function parseMe(board) {

var boardArray = board.split(" ");

var toPrint = "";
for (var j = 0; j < boardArray.length; j++) {
toPrint += boardArray[j];
}
document.getElementById("test").innerHTML = "test" + toPrint;


var board2D = [
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"],
["0", "0", "0", "0", "0", "0", "0", "0", "0"]
];

var counter = 0;
for (var i = 0; i < board2D.length; i++) {
for (var j = 0; j < board2D.length; j++) {
board2D[i][j] = boardArray[counter];
counter++;
}
}
if (counter != 81) {
document.getElementById("error").innerHTML = "Invalid entry, should be 81 total digits\n";
}
printBoard(board2D);
}

function printBoard(board) {
var toPrint = "";

for (var i = 0; i < board.length; i++) {
for (var j = 0; j < board.length; j++) {
toPrint += board[i][j] + " ";
}
toPrint += "\n";
}
document.getElementById("printBoard").innerHTML = toPrint;
}

import1();
<div id="demo"></div>
<div id="test"></div>
<div id="printBoard"></div>
<div id="error"></div>

关于javascript - Javascript数组中的魔术逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51775533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com