Skip to content
Next Next commit
Improve N-Queens input validation and solution counting clarity
  • Loading branch information
MayankSharma-2812 committed Jan 22, 2026
commit 22399def5e83ca7a9a128a1236d0b6c01f880b1d
9 changes: 4 additions & 5 deletions Backtracking/NQueens.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class NQueens {
constructor(size) {
if (size < 0) {
throw RangeError('Invalid board size')
if (size <= 0) {
throw RangeError('Board size must be a positive integer')
}

this.board = new Array(size).fill('.').map(() => new Array(size).fill('.'))
this.size = size
this.solutionCount = 0
Expand Down Expand Up @@ -40,7 +41,7 @@ class NQueens {
solve(col = 0) {
if (col >= this.size) {
this.solutionCount++
return true
return
}

for (let i = 0; i < this.size; i++) {
Expand All @@ -50,8 +51,6 @@ class NQueens {
this.removeQueen(i, col)
}
}

return false
}

printBoard(output = (value) => console.log(value)) {
Expand Down