DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Panagrams

function pangrams(s) {
    // Write your code here
   var lowerInput = s.toLowerCase();
    var letterArray = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
    for(var i = 0, l = letterArray.length; i < l; i++) {
        if(lowerInput.toLowerCase().indexOf(letterArray[i]) == -1) {
            console.log('not pangram');
            return "not pangram";
        }
    }
    return 'pangram';
} 
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
kamil7x profile image
Kamil Trusiak

I think this could be done in simpler way

This works for English alphabet, for others you should change 26 to number of letters in that alphabet.

function isPangram(str) {
    // Remove all chars except letters
    const onlyLetters = str.toLowerCase().replace(/[^a-z]+/g, ''); 

    // Create set from letters array 
    const letterSet = new Set(onlyLetters.split('')); 

    // Check if all letters are used
    return letterSet.size === 26; 
}
Enter fullscreen mode Exit fullscreen mode