Skip to content

Commit 83c5131

Browse files
committed
feat: add two implemetaion of
The first implementation with regex and second via HashSet & add the test code for both
1 parent eb624c2 commit 83c5131

2 files changed

Lines changed: 72 additions & 16 deletions

File tree

String/CheckPangram.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* @function checkPangram
3-
* @description - Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram
2+
* @function checkPangramRegex
3+
* @description - This function check pangram with the help of regex pattern
44
* @param {string} string
55
* @returns {boolean}
6-
* @example - checkPangram("'The quick brown fox jumps over the lazy dog' is a pangram") => true
7-
* @example - checkPangram('"Waltz, bad nymph, for quick jigs vex." is a pangram') => true
6+
* @example - checkPangramRegex("'The quick brown fox jumps over the lazy dog' is a pangram") => true
7+
* @example - checkPangramRegex('"Waltz, bad nymph, for quick jigs vex." is a pangram') => true
88
*/
9-
const checkPangram = (string) => {
9+
const checkPangramRegex = (string) => {
1010
if (typeof string !== 'string') {
1111
throw new TypeError('The given value is not a string')
1212
}
@@ -23,4 +23,28 @@ const checkPangram = (string) => {
2323
return string.match(/([a-z])(?!.*\1)/gi).length === 26
2424
}
2525

26-
export { checkPangram }
26+
27+
/**
28+
* @function checkPangramSet
29+
* @description - This function detect the pangram sentence by HashSet
30+
* @param {string} string
31+
* @returns {boolean}
32+
*/
33+
const checkPangramSet = (string) => {
34+
if (typeof string !== 'string') {
35+
throw new TypeError('The given value is not a string')
36+
}
37+
38+
const lettersSet = new Set()
39+
40+
for (const letter of string.toUpperCase()) {
41+
if (/[A-Z]/.test(letter)) {
42+
// if the letter is a valid uppercase alphabet then the add method insert the letter to the HashSet
43+
lettersSet.add(letter)
44+
}
45+
}
46+
47+
return lettersSet.size === 26
48+
}
49+
50+
export { checkPangramRegex, checkPangramSet}

String/test/CheckPangram.test.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,65 @@
1-
import { checkPangram } from '../CheckPangram'
1+
import { checkPangramRegex, checkPangramSet } from '../CheckPangram'
22

3-
describe('checkPangram', () => {
3+
describe('Testing checkPangramRegex function', () => {
44
it('"The quick brown fox jumps over the lazy dog" is a pangram', () => {
55
expect(
6-
checkPangram('The quick brown fox jumps over the lazy dog')
7-
).toBeTruthy()
6+
checkPangramRegex('The quick brown fox jumps over the lazy dog')
7+
).toBe(true)
88
})
99

1010
it('"Waltz, bad nymph, for quick jigs vex." is a pangram', () => {
11-
expect(checkPangram('Waltz, bad nymph, for quick jigs vex.')).toBeTruthy()
11+
expect(checkPangramRegex('Waltz, bad nymph, for quick jigs vex.')).toBe(true)
1212
})
1313

1414
it('"Jived fox nymph grabs quick waltz." is a pangram', () => {
15-
expect(checkPangram('Jived fox nymph grabs quick waltz.')).toBeTruthy()
15+
expect(checkPangramRegex('Jived fox nymph grabs quick waltz.')).toBe(true)
1616
})
1717

1818
it('"My name is Unknown" is NOT a pangram', () => {
19-
expect(checkPangram('My name is Unknown')).toBeFalsy()
19+
expect(checkPangramRegex('My name is Unknown')).toBe(false)
2020
})
2121

2222
it('"The quick brown fox jumps over the la_y dog" is NOT a pangram', () => {
2323
expect(
24-
checkPangram('The quick brown fox jumps over the la_y dog')
25-
).toBeFalsy()
24+
checkPangramRegex('The quick brown fox jumps over the la_y dog')
25+
).toBe(false)
2626
})
2727

2828
it('Throws an error if given param is not a string', () => {
2929
expect(() => {
30-
checkPangram(undefined)
30+
checkPangramRegex(undefined)
3131
}).toThrow('The given value is not a string')
3232
})
3333
})
34+
35+
describe('Testing checkPangramSet function', () => {
36+
it('"The quick brown fox jumps over the lazy dog" is a pangram', () => {
37+
expect(
38+
checkPangramSet('The quick brown fox jumps over the lazy dog')
39+
).toBe(true)
40+
})
41+
42+
it('"Waltz, bad nymph, for quick jigs vex." is a pangram', () => {
43+
expect(checkPangramSet('Waltz, bad nymph, for quick jigs vex.')).toBe(true)
44+
})
45+
46+
it('"Jived fox nymph grabs quick waltz." is a pangram', () => {
47+
expect(checkPangramSet('Jived fox nymph grabs quick waltz.')).toBe(true)
48+
})
49+
50+
it('"My name is Unknown" is NOT a pangram', () => {
51+
expect(checkPangramSet('My name is Unknown')).toBe(false)
52+
})
53+
54+
it('"The quick brown fox jumps over the la_y dog" is NOT a pangram', () => {
55+
expect(
56+
checkPangramSet('The quick brown fox jumps over the la_y dog')
57+
).toBe(false)
58+
})
59+
60+
it('Throws an error if given param is not a string', () => {
61+
expect(() => {
62+
checkPangramSet(undefined)
63+
}).toThrow('The given value is not a string')
64+
})
65+
})

0 commit comments

Comments
 (0)