Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
Not able to use a method inside a class

I am trying to make a matrix class. The cof() function is not working when I am using it as a method and showing this error.
practice:47 Uncaught TypeError: Cannot read property 'cof' of undefined.

But when I am using it inside det() method as a function it is working perfectly. Can anyone explain why this is happening?

class Matrix{
 
 constructor(matrix){
     this.matrix = matrix
 }   
 
 cof(matrix = this.matrix, i, j) {
    let cofMat = matrix.map(row =>
      row.filter((_, colIndex) => j !== colIndex)
    )
    cofMat.splice(cofMat[i],1)
    return cofMat
  }
     
 det(matrix = this.matrix) {
  let validInput = true;

  //input validation
  let columnLength = matrix.length;
  matrix.forEach((row) =>
    row.length === columnLength ? (validInput = true) : (validInput = false)
  );
  if (!validInput) return "Input a valid n*n matrix";

  // determining the matrix using 1st row.

  // This function is not working properly as a method

  function cof(matrix, i, j) {
    let cofMat = matrix.map(row =>
      row.filter((_, colIndex) => j !== colIndex)
    )
    cofMat.splice(cofMat[i],1)
    return cofMat
  }

  function recursiveDeterminantMatrix(matrix) {
    if (matrix.length === 2 && matrix[0].length === 2) {
      let result = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
      return result;
    } else {
      let answer = 0;
      for(let i =0; i< matrix.length; i++) {
        let cofactor =
          (-1) ** i *
          matrix[0][i] *
          recursiveDeterminantMatrix(this.cof(matrix, 0, i));
        answer += cofactor;
      };
      return answer;
    }
  }

  return recursiveDeterminantMatrix(matrix);
 }  
}

let matrix = [[1,2,3],[4,5,6],[7,8,8]];

let mat = new Matrix(matrix).det()
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

When the recursiveDeterminantMatrix function executes, this does not evaluate to the instance of the class, because it's not a method, just a plain function.

You could save the instance of the class in a self variable for proper use in the inner function.

det(matrix = this.matrix) {
 const self = this;

 // ...

 function recursiveDeterminantMatrix(matrix) {
   if (matrix.length === 2 && matrix[0].length === 2) {
     let result = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
     return result;
   } else {
     let answer = 0;
     for(let i =0; i< matrix.length; i++) {
       let cofactor =
         (-1) ** i *
         matrix[0][i] *
         recursiveDeterminantMatrix(self.cof(matrix, 0, i));
       answer += cofactor;
     };
     return answer;
   }
 }

 return recursiveDeterminantMatrix(matrix);
} 

For more information about this keyword, please check the MDN Docs

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use bind for this.

recursiveDeterminantMatrix(this.cof(matrix, 0, i).bind(this));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda