File tree Expand file tree Collapse file tree 2 files changed +7
-7
lines changed Expand file tree Collapse file tree 2 files changed +7
-7
lines changed Original file line number Diff line number Diff line change @@ -12,16 +12,16 @@ import { PrimeCheck } from '../Maths/PrimeCheck.js'
12
12
*/
13
13
export const nthPrime = ( n ) => {
14
14
if ( n < 1 ) {
15
- return 'Invalid Input'
15
+ throw new Error ( 'Invalid Input' )
16
16
}
17
17
18
18
let count = 0
19
- let inc = 2
19
+ let candidateValue = 2
20
20
while ( count < n ) {
21
- if ( PrimeCheck ( inc ) ) {
21
+ if ( PrimeCheck ( candidateValue ) ) {
22
22
count ++
23
23
}
24
- inc ++
24
+ candidateValue ++
25
25
}
26
- return inc - 1
26
+ return candidateValue - 1
27
27
}
Original file line number Diff line number Diff line change @@ -2,10 +2,10 @@ import { nthPrime } from '../Problem007.js'
2
2
3
3
describe ( 'checking nth prime number' , ( ) => {
4
4
it ( 'should be invalid input if number is negative' , ( ) => {
5
- expect ( nthPrime ( - 3 ) ) . toBe ( 'Invalid Input' )
5
+ expect ( ( ) => nthPrime ( - 3 ) ) . toThrowError ( 'Invalid Input' )
6
6
} )
7
7
it ( 'should be invalid input if number is 0' , ( ) => {
8
- expect ( nthPrime ( 0 ) ) . toBe ( 'Invalid Input' )
8
+ expect ( ( ) => nthPrime ( 0 ) ) . toThrowError ( 'Invalid Input' )
9
9
} )
10
10
test ( 'if the number is greater than 0' , ( ) => {
11
11
expect ( nthPrime ( 10 ) ) . toBe ( 29 )
You can’t perform that action at this time.
0 commit comments