0

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
}

VLAZ
  • 26,331
  • 9
  • 49
  • 67
ZooQ
  • 11
  • 2
  • 2
    I don't think this question is exactly a dupe of the linked questions, though it wouldn't surprise me if it's a dupe of some other question. The issue here is that the OP was accidentally creating and modifying a global variable (= a property of the global object), which was therefore being shared among all recursive calls, such that "inner" calls were messing with the state of "outer" calls. Neither of the linked questions is really about that, even if one of them is broad enough that it arguably covers any sort of question about JavaScript variable declarations. – ruakh Jul 03 '22 at 09:36
  • This question: [Variable assignment avoids an infinite loop](https://stackoverflow.com/q/69014517) might be better suited – Nick Parsons Jul 03 '22 at 10:13

0 Answers0