Skip to content

Commit f90c5aa

Browse files
authored
Merge pull request #478 from R-Oscar/master
Add check pangram algorithm
2 parents 62d1e40 + 54198ac commit f90c5aa

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

String/CheckPangram.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Pangram is a sentence that contains all the letters in the alphabet
3+
https://en.wikipedia.org/wiki/Pangram
4+
*/
5+
6+
const checkPangram = (string) => {
7+
if (typeof string !== 'string') {
8+
throw new TypeError('The given value is not a string')
9+
}
10+
11+
const frequency = new Set()
12+
13+
for (const letter of string.toLowerCase()) {
14+
if (letter >= 'a' && letter <= 'z') {
15+
frequency.add(letter)
16+
}
17+
}
18+
19+
return frequency.size === 26
20+
}
21+
22+
export { checkPangram }

String/test/CheckPangram.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { checkPangram } from '../CheckPangram'
2+
3+
describe('checkPangram', () => {
4+
it('"The quick brown fox jumps over the lazy dog" is a pangram', () => {
5+
expect(
6+
checkPangram('The quick brown fox jumps over the lazy dog')
7+
).toBeTruthy()
8+
})
9+
10+
it('"Waltz, bad nymph, for quick jigs vex." is a pangram', () => {
11+
expect(checkPangram('Waltz, bad nymph, for quick jigs vex.')).toBeTruthy()
12+
})
13+
14+
it('"Jived fox nymph grabs quick waltz." is a pangram', () => {
15+
expect(checkPangram('Jived fox nymph grabs quick waltz.')).toBeTruthy()
16+
})
17+
18+
it('"My name is Unknown" is NOT a pangram', () => {
19+
expect(checkPangram('My name is Unknown')).toBeFalsy()
20+
})
21+
22+
it('"The quick brown fox jumps over the la_y dog" is NOT a pangram', () => {
23+
expect(
24+
checkPangram('The quick brown fox jumps over the la_y dog')
25+
).toBeFalsy()
26+
})
27+
28+
it('Throws an error if given param is not a string', () => {
29+
expect(() => {
30+
checkPangram(undefined)
31+
}).toThrow('The given value is not a string')
32+
})
33+
})

0 commit comments

Comments
 (0)