mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Renamed NQueen file, variables to NQueens, raised RangeError exception (#1162)
This commit is contained in:
@ -1,5 +1,8 @@
|
|||||||
class NQueen {
|
class NQueens {
|
||||||
constructor (size) {
|
constructor (size) {
|
||||||
|
if (size < 0) {
|
||||||
|
throw RangeError('Invalid board size')
|
||||||
|
}
|
||||||
this.board = new Array(size).fill('.').map(() => new Array(size).fill('.'))
|
this.board = new Array(size).fill('.').map(() => new Array(size).fill('.'))
|
||||||
this.size = size
|
this.size = size
|
||||||
this.solutionCount = 0
|
this.solutionCount = 0
|
||||||
@ -61,4 +64,4 @@ class NQueen {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { NQueen }
|
export { NQueens }
|
@ -1,15 +0,0 @@
|
|||||||
import { NQueen } from '../NQueen'
|
|
||||||
|
|
||||||
describe('NQueen', () => {
|
|
||||||
it('should return 2 solutions for 4x4 size board', () => {
|
|
||||||
const _4Queen = new NQueen(4)
|
|
||||||
_4Queen.solve()
|
|
||||||
expect(_4Queen.solutionCount).toEqual(2)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should return 92 solutions for 8x8 size board', () => {
|
|
||||||
const _8Queen = new NQueen(8)
|
|
||||||
_8Queen.solve()
|
|
||||||
expect(_8Queen.solutionCount).toEqual(92)
|
|
||||||
})
|
|
||||||
})
|
|
19
Backtracking/tests/NQueens.test.js
Normal file
19
Backtracking/tests/NQueens.test.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { NQueens } from '../NQueens'
|
||||||
|
|
||||||
|
describe('NQueens', () => {
|
||||||
|
it('should return 2 solutions for 4x4 size board', () => {
|
||||||
|
const _4Queens = new NQueens(4)
|
||||||
|
_4Queens.solve()
|
||||||
|
expect(_4Queens.solutionCount).toEqual(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return 92 solutions for 8x8 size board', () => {
|
||||||
|
const _8Queens = new NQueens(8)
|
||||||
|
_8Queens.solve()
|
||||||
|
expect(_8Queens.solutionCount).toEqual(92)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw RangeError for negative size board', () => {
|
||||||
|
expect(() => { return new NQueens(-1) }).toThrow(RangeError)
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user