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

163
Vistas
Visibility of arrays in javascript ("undefined")

I tried to calculate the sum of two matrizes (multi-dimensional-arrays), but I get the following error-message:

Uncaught TypeError: Cannot set properties of undefined (setting '0')
    at matAdd (matrixCalc.js:28)

when I do this code↓. I don't understand why "matSum[0][0]" is undefined.

// M1 + M2
function matAdd(m1, m2){        
    let matSum = new Array(m1.length);
    for (let i=0; i<m1.length; i++){       //create a blanco-matrix
        matSum=new Array(m1[0].length);
    }
    
    if (m1.length == m2.length && m1[0].length==m2[0].length){
        for (let i=0; i<m1.length; i++){
            for (let j=0; j<m1[0].length; j++){
                matSum[i][j]=m1[i][j]+m2[i][j];                  //HERE THE ERROR OCCURS
            }
        }
    }
    else console.log("Dimension-Error")
    return matSum;
} 

the code with line-numbers

Thx for the help :)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try to initiate your matrix like this

function matAdd(m1, m2){        
    let matSum = new Array(m1.length);
    for (let i=0; i<m1.length; i++){       //create a blanco-matrix
        matSum [i] =new Array(m1[0].length); // for each column
    }
    
    if (m1.length == m2.length && m1[0].length==m2[0].length){
        for (let i=0; i<m1.length; i++){
            for (let j=0; j<m1[0].length; j++){
                matSum[i][j]=m1[i][j]+m2[i][j];                  //HERE THE ERROR OCCURS
            }
        }
    }
    else console.log("Dimension-Error")
    return matSum;
} 
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