mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
Apply standard code style
Sadly, standard does not support private fields (yet)
This commit is contained in:
@ -108,13 +108,6 @@ function getPath (grid) {
|
|||||||
* Creates an instance of the "rat in a maze" based on a given grid (maze).
|
* Creates an instance of the "rat in a maze" based on a given grid (maze).
|
||||||
*/
|
*/
|
||||||
export class RatInAMaze {
|
export class RatInAMaze {
|
||||||
|
|
||||||
/** Path from the source [0, 0] to the target [N-1, N-1]. */
|
|
||||||
#_path = ''
|
|
||||||
|
|
||||||
/** Whether the rat could find a way to the target or not. */
|
|
||||||
#_solved = false
|
|
||||||
|
|
||||||
constructor (grid) {
|
constructor (grid) {
|
||||||
// first, let's do some error checking on the input
|
// first, let's do some error checking on the input
|
||||||
validateGrid(grid)
|
validateGrid(grid)
|
||||||
@ -123,17 +116,11 @@ export class RatInAMaze {
|
|||||||
const solution = getPath(grid)
|
const solution = getPath(grid)
|
||||||
|
|
||||||
if (solution !== false) {
|
if (solution !== false) {
|
||||||
this.#_path = solution
|
this.path = solution
|
||||||
this.#_solved = true
|
this.solved = true
|
||||||
|
} else {
|
||||||
|
this.path = ''
|
||||||
|
this.solved = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get solved () {
|
|
||||||
return this.#_solved
|
|
||||||
}
|
|
||||||
|
|
||||||
get path () {
|
|
||||||
return this.#_path
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,15 @@ describe('RatInAMaze', () => {
|
|||||||
const values = [undefined, null, {}, 42, 'hello, world']
|
const values = [undefined, null, {}, 42, 'hello, world']
|
||||||
|
|
||||||
for (const value of values) {
|
for (const value of values) {
|
||||||
|
// we deliberately want to check whether this constructor call fails or not
|
||||||
|
// eslint-disable-next-line no-new
|
||||||
expect(() => { new RatInAMaze(value) }).toThrow()
|
expect(() => { new RatInAMaze(value) }).toThrow()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should fail for an empty array', () => {
|
it('should fail for an empty array', () => {
|
||||||
|
// we deliberately want to check whether this constructor call fails or not
|
||||||
|
// eslint-disable-next-line no-new
|
||||||
expect(() => { new RatInAMaze([]) }).toThrow()
|
expect(() => { new RatInAMaze([]) }).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -19,6 +23,8 @@ describe('RatInAMaze', () => {
|
|||||||
[0, 0]
|
[0, 0]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
// we deliberately want to check whether this constructor call fails or not
|
||||||
|
// eslint-disable-next-line no-new
|
||||||
expect(() => { new RatInAMaze(array) }).toThrow()
|
expect(() => { new RatInAMaze(array) }).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -26,6 +32,8 @@ describe('RatInAMaze', () => {
|
|||||||
const values = [[[2]], [['a']]]
|
const values = [[[2]], [['a']]]
|
||||||
|
|
||||||
for (const value of values) {
|
for (const value of values) {
|
||||||
|
// we deliberately want to check whether this constructor call fails or not
|
||||||
|
// eslint-disable-next-line no-new
|
||||||
expect(() => { new RatInAMaze(value) }).toThrow()
|
expect(() => { new RatInAMaze(value) }).toThrow()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user