I am currently learning TypeScript from scratch by working on exercises available on exercism
Successfully completed the 5th exercise on Pangram.
Below is my solution:
class Pangram {
alphabet = "abcdefghijklmnopqrstuvwxyz"
constructor(private pangram:string) {
this.pangram = pangram.toLowerCase().normalize()
}
isPangram():boolean{
for( let letter of this.alphabet){
if(this.pangram.indexOf(letter) < 0)
return false
}
return true;
}
}
export default Pangram
To enhance my skills, I am reviewing other solutions to learn more. One particular solution caught my attention because it involves regex, which I'm not yet proficient in:
class Pangram {
constructor(private text:string) {
}
isPangram():boolean {
var set = new Set(this.text.toLowerCase().replace(/[^A-Za-z]/g, ''))
return set.size == 26
}
}
export default Pangram
I'm a bit confused about how the second solution works. Initially, I thought it would replace all letters with empty characters, hence having a size of zero. However, it checks for a size of 26. Without knowing how to debug, I can only run tests to confirm its accuracy, which has been successful so far.
If anyone could explain what's happening in the second solution, I'd appreciate it!
For those curious, I've included the unit tests used for this assignment:
import Pangram from './pangram'
describe('Pangram()', () => {
// Test cases here...
})
Thank you!