This is the second algorithm I’m writing about and it’s about a function that returns true if the string passed is a pangram and false if is not.
Firstly, we need to find out what is a pangram.
According to Google, pangram is:
Now that we know what a pangram is, we need to create some tests to see if our work is valid.
As simple as that… Two tests are enough to test the behaviour of our function.
The logic behind this algorithm is actually very simple. We need to check if any letter of an alphabet array can be found in the passed string. If there is any letter that cannot be found, we need to return false.
To do that we are going to need two arrays:
- alphabet array
- an array with only the letters from the input
Let’s start with the alphabet array:
const abc = "abcdefghijklmnopqrstuvwxyz".split("");
For the other array, I used a different approach:
Here I am just iterating over each word and create only one big array with all the letters.
The next step is the loop over
This is the main part of our function. I created a loop that will run for any letter of the alphabet array and do a short check. If a letter on any index cannot be found in the
arrayOfLetters the result array will be set to false.
Before this loop to begin we need to define a variable
result and set it to true.
After the loop, we only need to return the result value and our algorithm is working perfectly and it should look like this:
Our tests are passing! Hooray!