gpt4 book ai didi

JavaScript 程序崩溃页面

转载 作者:行者123 更新时间:2023-11-30 09:55:13 26 4
gpt4 key购买 nike

我用 JavaScript 编写了一个简单的程序来查找3X3 矩阵的余因子矩阵。但是,程序反复使我的页面崩溃,我在程序中找不到任何逻辑错误。这是我的代码:

var matrixA = {
a11:"",
a12:"",
a13:"",
a21:"",
a22:"",
a23:"",
a31:"",
a32:"",
a33:""
};

function determinant (given,order) {
if(order==3){
var det = (given.a11*((given.a22*given.a33)-(given.a23*given.a32)))-
(given.a21*((given.a12*given.a33)-(given.a13*given.a32)))+
(given.a31*((given.a12*given.a23)-(given.a13*given.a22)));
}
else if(order==2){
var det = (given.a11*given.a22)-(given.a21*given.a12);
}
return det;
}

function cofactors(given){
var multiplier;
var temp = {
a11:"",
a12:"",
a21:"",
a22:""
};
var found_cofactor_matrix = {};
for (var i = 1; i <4; i++) {
for (var j = 1; i < 4; j++) {
if(((i+j)%2)==0){
multiplier = 1;
}
else if(((i+j)%2)==1){
multiplier = -1;
}
//Check whether row or column number is the same to make a 2X2 matrix
for(var a = 1; a < 4; a++){
for (var b = 0; b < 4; b++) {
if((a==i)||(b==j)){

} //do nothing
else{
if(temp.a11==""){
temp.a11 = given["a"+a+b];
}
else if(temp.a12 == ""){
temp.a12 = given["a"+a+b];
}
else if(temp.a21 == ""){
temp.a21 = given["a"+a+b];
}
else if(temp.a22 == ""){
temp.a22 = given["a"+a+b];
}
}
};
};
found_cofactor_matrix["a"+i+j] = multiplier*determinant(temp,2);
};
};
return found_cofactor_matrix;
}

这里的参数“given”是从调用函数的地方传给它的对象,是要求余因子的原始矩阵(matrixA)。我基本上首先创建一个 2X2 矩阵,找到它的行列式并根据需要将其乘以 1 或 -1。然后我将这个值写入矩阵中的适当位置。

最佳答案

for (var j = 1; i < 4; j++) {应该是 for (var j = 1; j < 4; j++) { .

关于JavaScript 程序崩溃页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34410989/

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