Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda