From 1feae20c9914b9c3385cc500ca76fddeeb156cbb Mon Sep 17 00:00:00 2001 From: sarthakgupta1907-cell Date: Fri, 6 Nov 2026 10:22:50 +0530 Subject: [PATCH] feat: add isPangram algorithm --- String/IsPangram.js | 25 +++++++++++++++++++++++ String/test/IsPangram.test.js | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 String/IsPangram.js create mode 100644 String/test/IsPangram.test.js diff --git a/String/IsPangram.js b/String/IsPangram.js new file mode 100644 index 0000000000..7a93bd8fb9 --- /dev/null +++ b/String/IsPangram.js @@ -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 } diff --git a/String/test/IsPangram.test.js b/String/test/IsPangram.test.js new file mode 100644 index 0000000000..9f0700c2d0 --- /dev/null +++ b/String/test/IsPangram.test.js @@ -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) + }) +})