Skip to content

Commit 22399de

Browse files
Improve N-Queens input validation and solution counting clarity
1 parent 08d8c6b commit 22399de

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Backtracking/NQueens.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class NQueens {
22
constructor(size) {
3-
if (size < 0) {
4-
throw RangeError('Invalid board size')
3+
if (size <= 0) {
4+
throw RangeError('Board size must be a positive integer')
55
}
6+
67
this.board = new Array(size).fill('.').map(() => new Array(size).fill('.'))
78
this.size = size
89
this.solutionCount = 0
@@ -40,7 +41,7 @@ class NQueens {
4041
solve(col = 0) {
4142
if (col >= this.size) {
4243
this.solutionCount++
43-
return true
44+
return
4445
}
4546

4647
for (let i = 0; i < this.size; i++) {
@@ -50,8 +51,6 @@ class NQueens {
5051
this.removeQueen(i, col)
5152
}
5253
}
53-
54-
return false
5554
}
5655

5756
printBoard(output = (value) => console.log(value)) {

0 commit comments

Comments
 (0)