Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions String/IsPangram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @function isPangram
* @description A pangram is a sentence that contains every letter of
* the alphabet at least once. This function checks whether a given
* string is a pangram, ignoring case.
* @param {string} str - The input string
* @returns {boolean} - True if the string is a pangram, false otherwise
* @see https://en.wikipedia.org/wiki/Pangram
* @example isPangram("The quick brown fox jumps over the lazy dog") => true
* @example isPangram("Hello World") => false
*/
const isPangram = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Input must be a string')
}
const letters = new Set(
str
.toLowerCase()
.replace(/[^a-z]/g, '')
.split('')
)
return letters.size === 26
}

export { isPangram }
37 changes: 37 additions & 0 deletions String/test/IsPangram.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { isPangram } from '../IsPangram.js'

describe('isPangram', () => {
test("'The quick brown fox jumps over the lazy dog' is a pangram", () => {
expect(isPangram('The quick brown fox jumps over the lazy dog')).toBe(true)
})

test("'Hello World' is NOT a pangram", () => {
expect(isPangram('Hello World')).toBe(false)
})

test('empty string is NOT a pangram', () => {
expect(isPangram('')).toBe(false)
})

test('case insensitive check', () => {
expect(isPangram('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG')).toBe(true)
})

test('string with numbers and symbols but all letters is a pangram', () => {
expect(isPangram('1 The quick brown fox jumps over the lazy dog 2!')).toBe(
true
)
})

test('string missing one letter is NOT a pangram', () => {
expect(isPangram('The quick brown fox jumps over the lazy cat')).toBe(false)
})

test('throws TypeError for non-string input', () => {
expect(() => isPangram(123)).toThrow(TypeError)
})

test('throws TypeError for null input', () => {
expect(() => isPangram(null)).toThrow(TypeError)
})
})
Loading