Skip to content

Commit e9b5a6a

Browse files
👌 IMPROVE: changed variable name from inc to candidateValue and thrown error in case of invalid input
1 parent d3a3b33 commit e9b5a6a

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

‎Project-Euler/Problem007.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import { PrimeCheck } from '../Maths/PrimeCheck.js'
1212
*/
1313
export const nthPrime = (n) => {
1414
if (n < 1) {
15-
return 'Invalid Input'
15+
throw new Error('Invalid Input')
1616
}
1717

1818
let count = 0
19-
let inc = 2
19+
let candidateValue = 2
2020
while (count < n) {
21-
if (PrimeCheck(inc)) {
21+
if (PrimeCheck(candidateValue)) {
2222
count++
2323
}
24-
inc++
24+
candidateValue++
2525
}
26-
return inc - 1
26+
return candidateValue - 1
2727
}

‎Project-Euler/test/Problem007.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { nthPrime } from '../Problem007.js'
22

33
describe('checking nth prime number', () => {
44
it('should be invalid input if number is negative', () => {
5-
expect(nthPrime(-3)).toBe('Invalid Input')
5+
expect(() => nthPrime(-3)).toThrowError('Invalid Input')
66
})
77
it('should be invalid input if number is 0', () => {
8-
expect(nthPrime(0)).toBe('Invalid Input')
8+
expect(() => nthPrime(0)).toThrowError('Invalid Input')
99
})
1010
test('if the number is greater than 0', () => {
1111
expect(nthPrime(10)).toBe(29)

0 commit comments

Comments
 (0)