mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
Merge pull request #478 from R-Oscar/master
Add check pangram algorithm
This commit is contained in:
22
String/CheckPangram.js
Normal file
22
String/CheckPangram.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
Pangram is a sentence that contains all the letters in the alphabet
|
||||||
|
https://en.wikipedia.org/wiki/Pangram
|
||||||
|
*/
|
||||||
|
|
||||||
|
const checkPangram = (string) => {
|
||||||
|
if (typeof string !== 'string') {
|
||||||
|
throw new TypeError('The given value is not a string')
|
||||||
|
}
|
||||||
|
|
||||||
|
const frequency = new Set()
|
||||||
|
|
||||||
|
for (const letter of string.toLowerCase()) {
|
||||||
|
if (letter >= 'a' && letter <= 'z') {
|
||||||
|
frequency.add(letter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return frequency.size === 26
|
||||||
|
}
|
||||||
|
|
||||||
|
export { checkPangram }
|
33
String/test/CheckPangram.test.js
Normal file
33
String/test/CheckPangram.test.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import { checkPangram } from '../CheckPangram'
|
||||||
|
|
||||||
|
describe('checkPangram', () => {
|
||||||
|
it('"The quick brown fox jumps over the lazy dog" is a pangram', () => {
|
||||||
|
expect(
|
||||||
|
checkPangram('The quick brown fox jumps over the lazy dog')
|
||||||
|
).toBeTruthy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('"Waltz, bad nymph, for quick jigs vex." is a pangram', () => {
|
||||||
|
expect(checkPangram('Waltz, bad nymph, for quick jigs vex.')).toBeTruthy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('"Jived fox nymph grabs quick waltz." is a pangram', () => {
|
||||||
|
expect(checkPangram('Jived fox nymph grabs quick waltz.')).toBeTruthy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('"My name is Unknown" is NOT a pangram', () => {
|
||||||
|
expect(checkPangram('My name is Unknown')).toBeFalsy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('"The quick brown fox jumps over the la_y dog" is NOT a pangram', () => {
|
||||||
|
expect(
|
||||||
|
checkPangram('The quick brown fox jumps over the la_y dog')
|
||||||
|
).toBeFalsy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Throws an error if given param is not a string', () => {
|
||||||
|
expect(() => {
|
||||||
|
checkPangram(undefined)
|
||||||
|
}).toThrow('The given value is not a string')
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user