处理错误“预期语法错误;但发现我”

时间:2018-11-07 16:18:41

标签: javascript matrix processing

我正在尝试使用javascript在处理中编写矩阵库,但始终在第5行出现错误。我似乎无法找出导致错误的原因,因此将不胜感激。

目标是实现矩阵乘积函数。

function Matrix(rows,cols){
    this.rows = rows;
    this.cols = cols;
    this.data = [];
    
    for(var i = 0; i < this.rows; i++){
      //assign every row an array
      this.data[i] = [];
      for (var j = 0; j < this.cols; j++){
        //assign every column an array
        this.data[i][j] = 0;
      }
    }
  }
  
  Matrix.prototype.multiply = function(n){
    if(n instanceof Matrix){
      
    // Matrix product
    if (this.cols !== n.rows) {
      console.log('Columns of A must match rows of B.');
      return undefined;
    }
    let result = new Matrix(this.rows, n.cols);
    for (let i = 0; i < result.rows; i++) {
      for (let j = 0; j < result.cols; j++) {
        // Dot product of values in col
        let sum = 0;
        for (let k = 0; k < this.cols; k++) {
          sum += this.data[i][k] * n.data[k][j];
        }
        result.data[i][j] = sum;
      }
    }
    return result;
  }
    
    else{
      for(var i = 0; i < this.rows; i++){
       for (var j = 0; j < this.cols; j++){
         //multiply scalar
         this.data[i][j] *= n;
      }
    }
   }
  }

1 个答案:

答案 0 :(得分:0)

我发现错误是在错误的地方只是一个花括号!