Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!