Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中发生意外值更改 var li=[[1,3,4,5],[1,2,3,1],[3,5,6,2]; var m=r4dxy(0.1); var i=0; li[i]=m.ope(li[0]); 文件.编写(i);_Javascript - Fatal编程技术网

javascript中发生意外值更改 var li=[[1,3,4,5],[1,2,3,1],[3,5,6,2]; var m=r4dxy(0.1); var i=0; li[i]=m.ope(li[0]); 文件.编写(i);

javascript中发生意外值更改 var li=[[1,3,4,5],[1,2,3,1],[3,5,6,2]; var m=r4dxy(0.1); var i=0; li[i]=m.ope(li[0]); 文件.编写(i);,javascript,Javascript,为什么上面代码中的变量“i”会更改为5 这是文件中的一个错误,它在matrixMul方法的下面循环中创建了一个全局变量i <body> <script src="http://www.cse.msu.edu/~zhangh40/w/wgl.js"></script> <script> var li = [[1,3,4,5],[1,2,3,1],[3,5,6,2]]; var m = r4dxy(0.1); var i =

为什么上面代码中的变量“i”会更改为5

这是文件中的一个错误,它在
matrixMul
方法的下面循环中创建了一个全局变量
i

<body>
<script src="http://www.cse.msu.edu/~zhangh40/w/wgl.js"></script>
<script>

    var li = [[1,3,4,5],[1,2,3,1],[3,5,6,2]];
    var m = r4dxy(0.1);
    var i = 0;
    li[i] = m.ope(li[0]);
    document.write(i);

</script>
因为中的代码编写不正确

该代码的功能是:

function matrixMul(other) {
    var i, a, b;
    if (this.c != other.r)
        return false;
    li = [];
    for (i = 1; i <= this.r * other.c; i++) {
        a = (i % other.c == 0) ? other.c : (i % other.c);
        b = Math.floor((i - 1) / other.c + 1);
        sum = 0;
        for (j = 1; j <= other.r; j++)
            sum += (this.at(b, j) * other.at(j, a));
        li.push(sum);
    }
    return new matrix(li, this.r, other.c);
}
函数矩阵MUL(其他){
if(this.c!=other.r)
返回false;
li=[];

对于(i=1;Ilaso,变量
a
b
li
、和
sum
也需要
var
for (var i = 1; i <= this.r * other.c; i++) {
function matrixMul(other) {
    var i, a, b;
    if (this.c != other.r)
        return false;
    li = [];
    for (i = 1; i <= this.r * other.c; i++) {
        a = (i % other.c == 0) ? other.c : (i % other.c);
        b = Math.floor((i - 1) / other.c + 1);
        sum = 0;
        for (j = 1; j <= other.r; j++)
            sum += (this.at(b, j) * other.at(j, a));
        li.push(sum);
    }
    return new matrix(li, this.r, other.c);
}
function matrixMul(other){
    if(this.c != other.r)
        return false;
    li = [];
    for(i=1;i<=this.r*other.c;i++){
        a = (i%other.c==0)?other.c:(i%other.c);
        b = Math.floor((i-1)/other.c+1);
        sum = 0;
        for(j=1;j<=other.r;j++)
            sum+=(this.at(b,j)*other.at(j,a));
        li.push(sum);
    }
    return new matrix(li,this.r,other.c);
}