[英]How to make a function that can convert any string that looks like javascript code to objects?
[英]“How can I code a 2 dimension array input in Javascript such that it looks like this?”
基本上我想做的是用Javascript开发的高斯消除程序,到目前为止,当我直接在代码中输入系数矩阵时,它就可以工作,但是现在我试图让用户输入它,但是我失败了。 例如,如果我直接写
$A = [[1, 1, 1], [2, 1, 2], [1, 2, 3]];
该程序可以正常运行,但是,按照我输入矩阵的方法,它将无法正常工作。
var rows= prompt("Input the number of variables");
var A = [];
alert("Input the coefficients of the equations");
for(var i = 0; i<rows; i++)
{
A.push([]);
A[i].push(new Array(rows));
for (var j = 0; j<rows; j++)
{
A[i][j]= prompt()
}
}
}
有没有办法正确编码?
您唯一的问题似乎是在代码末尾有一个额外的}
。 也许使用一致的段落缩进格式重新格式化将在将来阐明这种错误。 请参阅下面的工作代码。
var rows = prompt("Input the number of variables"); var A = []; alert("Input the coefficients of the equations"); for(var i = 0; i<rows; i++) { A.push([]); A[i].push(new Array(rows)); for (var j = 0; j<rows; j++) { A[i][j]= prompt() } } console.log(A);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.