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

123
Vistas
Javascript for loop odd behavior

I'm writing an algorithm to solve a sudoku puzzle and I'm getting some weird behavior. My first draft below did not give me an error, but it was modifying the array. Then I realized (because I am new to JavaScript) that I have to put let before the var in the for loop, so I changed it.

The weird behavior is that when I change it to let row and let col in the for loop it no longer modifies the puzzle param array. I do not understand how adding the let word to the for loop changes this?

test = [
  [8, 0, 6, 0, 1, 0, 0, 0, 0],
  [0, 0, 3, 0, 6, 4, 0, 9, 0],
  [9, 0, 0, 0, 0, 0, 8, 1, 6],
  [0, 8, 0, 3, 9, 6, 0, 0, 0],
  [7, 0, 2, 0, 4, 0, 3, 0, 9],
  [0, 0, 0, 5, 7, 2, 0, 8, 0],
  [5, 2, 1, 0, 0, 0, 0, 0, 4],
  [0, 3, 0, 7, 5, 0, 2, 0, 0],
  [0, 0, 0, 0, 2, 0, 1, 0, 5]
]

let result = solve(test)
console.log(result)
printPuzzle(test)

function solve(puzzle) {

  for (row = 0; row < 9; row++) {
    for (col = 0; col < 9; col++) {

      if (puzzle[row][col] === 0) {

        for (number = 1; number < 9; number++) {

          if (isValidPlacement(puzzle, number, row, col)) {

            puzzle[row][col] = number

            if (solve(puzzle)) {
              return true
            } else {
              puzzle[row][col] = 0
            }

          }

        }

        return false

      }

    }
  }

  return true

}

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