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

112
Vistas
What is the difference between using let keyword in a for loop and not using let keyword while declaring iterative variable?

I was doing a determinant challenge on codewars when I encountered this issue. Apparently, my for loop for i was running only once when the matrix was 4x4 or bigger but it was running fine for a 3x3 matrix. But when I put let before i, it worked for a matrix of any size (3x3 or bigger). I'm not an experienced JS programmer, so, I'd love it if someone could explain the difference and/or reason why my program was acting in this way.


//Buggy code
const determinant = (matrix) => {
    if(matrix[0].length === 1) return matrix[0][0]
    if(matrix[0].length === 2) return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]))
    sum = 0
    for(i = 0; i < matrix[0].length; i++){
        console.log(matrix, i)
        subMat = []
        for(j = 1; j < matrix.length; j++) {
            subMat2 = []
            for(k = 0; k < matrix[0].length; k++){
                if(i === k) {}
                else subMat2.push(matrix[j][k])
                // else if(matrix[j][k] !== matrix[0][i])
            }
            console.log(subMat2)
            subMat.push(subMat2)
        } 
        if(i % 2 === 0) sum += matrix[0][i] * (determinant(subMat))
        else sum += -(matrix[0][i] * (determinant(subMat)))
        console.log(subMat, sum, matrix[0].length)
    }
    return sum
}

//Correct code
const determinantCorrected = (matrix) => {
    if(matrix[0].length === 1) return matrix[0][0]
    if(matrix[0].length === 2) return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]))
    sum = 0
    for(let i = 0; i < matrix[0].length; i++){
        console.log(matrix, i)
        subMat = []
        for(j = 1; j < matrix.length; j++) {
            subMat2 = []
            for(k = 0; k < matrix[0].length; k++){
                if(i === k) {}
                else subMat2.push(matrix[j][k])
                // else if(matrix[j][k] !== matrix[0][i])
            }
            console.log(subMat2)
            subMat.push(subMat2)
        } 
        if(i % 2 === 0) sum += matrix[0][i] * (determinant(subMat))
        else sum += -(matrix[0][i] * (determinant(subMat)))
        console.log(subMat, sum, matrix[0].length)
    }
    return sum
}

about 4 years ago · Juan Pablo Isaza
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