# Weekly Coding Challenge: Detect Pangram – written in JavaScript

Posted by

Hello readers! It’s really important for me to continue my practice and learning. Lately, I have been completing a coding challenge a day with codewars. This helps me keep my skills sharp and see how other devs all over the planet would solve these problems. Being able to see other solutions expands my mind for solving future problems in a clever and efficient way. For this problem set, I’ve decided to only write it in JavaScript. This problem is a great opportunity for us to review a long way to complete a problem and then how to utilize some of the ES8 and ES9 functions to simplify it significantly. Here is the problem:

Detect Pangram

A pangram is a sentence that contains every single letter of the alphabet at least once. For example, the sentence “The quick brown fox jumps over the lazy dog” is a pangram, because it uses the letters A-Z at least once (case is irrelevant).

Given a string, detect whether or not it is a pangram. Return True if it is, False if not. Ignore numbers and punctuation.

Here is basically a long division way of how you could solve this problem:

Drawn out JavaScript solution:

```function isPangram(string){
// Create a string of the alphabet
let alpha = 'abcdefghijklmnopqrstuvwxyz';
// Generate an empty object
let alphaObj = {};
// Boolean that we will eventually return
let allLetters;
// Loop over the string and construct an object with letter to integer for counts
for (let i = 0; i < alpha.length; i++) {
alphaObj[alpha.charAt(i)] = 0;
}

// Now we can condense the actual parameter value to a giant string of letters
let condense = string.replace(/\s/g, '').split(",").toString()
// After, we can loop over the string and if the alphaObj contains the property, increment the count
for (let i = 0; i < condense.length; i++) {
if (alphaObj.hasOwnProperty(condense.charAt(i).toLowerCase())) {
alphaObj[condense.charAt(i).toLowerCase()] += 1;
}
}

// finally, loop over the object and if all object properties have an amount that’s greater than 1, we can return true
for (let item in alphaObj) {
if (alphaObj[item] > 0) {
allLetters = true;
} else {
allLetters = false;
break;
}
}
return allLetters;
}
```

As you can see, this is a pretty drawn out solution but it gives you an idea of how we could use some basic vanilla JavaScript capabilities to determine if our string contains all of the values in the alphabet. However, there is a MUCH easier way to do this using the match() function in JavaScript with some light regex.

Pretty JavaScript

```function isPangram(string){
// Matching our string to letters in the alphabet
const letters = string.toLowerCase().match(/[a-z]/g);
// Add those letters to a Set to deduplicate
const alphabet = [...new Set(letters)]
// Assert that the length of the variable is 26
return alphabet.length === 26;
}
```

The whole takeaway is that while we can complete work with a vanilla solution, we can utilize out of the box functions to make our life way easier. Granted, you could solve the first piece in many different ways. So I encourage you to spend time reading about out of the box JS functions that you can leverage in your everyday life. They really are useful!

All credit for the problem sets go to codewars