Skip to content

Commit 1feae20

Browse files
feat: add isPangram algorithm
1 parent 5c39e87 commit 1feae20

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎String/IsPangram.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @function isPangram
3+
* @description A pangram is a sentence that contains every letter of
4+
* the alphabet at least once. This function checks whether a given
5+
* string is a pangram, ignoring case.
6+
* @param {string} str - The input string
7+
* @returns {boolean} - True if the string is a pangram, false otherwise
8+
* @see https://en.wikipedia.org/wiki/Pangram
9+
* @example isPangram("The quick brown fox jumps over the lazy dog") => true
10+
* @example isPangram("Hello World") => false
11+
*/
12+
const isPangram = (str) => {
13+
if (typeof str !== 'string') {
14+
throw new TypeError('Input must be a string')
15+
}
16+
const letters = new Set(
17+
str
18+
.toLowerCase()
19+
.replace(/[^a-z]/g, '')
20+
.split('')
21+
)
22+
return letters.size === 26
23+
}
24+
25+
export { isPangram }

‎String/test/IsPangram.test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { isPangram } from '../IsPangram.js'
2+
3+
describe('isPangram', () => {
4+
test("'The quick brown fox jumps over the lazy dog' is a pangram", () => {
5+
expect(isPangram('The quick brown fox jumps over the lazy dog')).toBe(true)
6+
})
7+
8+
test("'Hello World' is NOT a pangram", () => {
9+
expect(isPangram('Hello World')).toBe(false)
10+
})
11+
12+
test('empty string is NOT a pangram', () => {
13+
expect(isPangram('')).toBe(false)
14+
})
15+
16+
test('case insensitive check', () => {
17+
expect(isPangram('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG')).toBe(true)
18+
})
19+
20+
test('string with numbers and symbols but all letters is a pangram', () => {
21+
expect(isPangram('1 The quick brown fox jumps over the lazy dog 2!')).toBe(
22+
true
23+
)
24+
})
25+
26+
test('string missing one letter is NOT a pangram', () => {
27+
expect(isPangram('The quick brown fox jumps over the lazy cat')).toBe(false)
28+
})
29+
30+
test('throws TypeError for non-string input', () => {
31+
expect(() => isPangram(123)).toThrow(TypeError)
32+
})
33+
34+
test('throws TypeError for null input', () => {
35+
expect(() => isPangram(null)).toThrow(TypeError)
36+
})
37+
})

0 commit comments

Comments
 (0)