Free Code Camp Missing Letter Algorithm Solution

jackLyons Free Code Camp Tags:
0
Missing Letter Algorithm image

In this post I will describe the method I used to solve the Free Code Camp Missing Letter AlgorithmIt isn’t too tricky, but this is the sort of algorithm challenge that can trip you up if you don’t read the question carefully. Luckily, I’ll show you a nice and easy-to-understand method.

To solve this challenge you are give the following hints:

But to be honest, I didn’t actually need them,instead I used the simple indexOf() method. Of course, you may not even need the above suggestions. If that’s the case, please do share your solution in the comments box below 🙂

How to solve the Missing Letter challenge:

Step 1: Read the question properly and prepare an outline

When I first came across this challenge, I got stuck quickly because I didn’t know a good method to compare alphabet letters against each other. I think it was because of the FCC suggestions to use the CharCodeAt() method. The thing I like about JavaScript is that there no solution fits all. You need to figure out what works best for you. There is nothing more satisfying then solving a challenge and actually understanding every single line of code that you wrote.

Once you have figured a problem out, you most certainly should go back and refactor. You can do this by googling around for other answers and reading up on how the solutions work. There is a great website called Code Wars which lets you complete code challenges and review other peoples solutions. This is a great way to see just flow flexible JS can be!

In order to read the question “properly”, be sure to check out the test cases. Because at the end of the day, these are the answers you are looking to solve. Take a look at the last two test cases in this challenge:


fearNotLetter("bcd") should return undefined
fearNotLetter("yz")  should return undefined
Can you see a pattern here? Baszically these cases are saying: if the string does not begin with “a”, then return undefinedAha! A pretty big loop-hole in my opinion.

Step 2: Create an alphabet to compare against

To begin this Missing Letter Algorithm challenge, all I did was create a new string called alpha. This variable held every letter in the alphabet. I also created another variable called result, to hold the missing letter once it has been found.


var alpha  = 'abcdefghijklmnopqrstuvwxyz';
var result = '';

Step 3: Loop over the string and compare to the alphabet

Using a for loop you can easily check each letter of the given string and compare it against the alphabet. To compare I used the indexOf() method. If the letters in the string do not match the letters in the alphabet, then a negative value will be returned. In this case, we store that value in the result variable. To check this, you can simply look for a return number less than 0. Take a look:


for (var i = 0; i < str.length; i++) {
    
     if (str[i].indexOf(alpha[i]) < 0) {
      result = alpha[i];
      break;
    } 

}

The loop is broken once an invalid result is found.

Step 4: Consider undefined results

Last but not least, you need to consider the undefined test cases, as mentioned above in Step 1. In my opinion, this challenge opens up a bit of a loop-hole because in the “real world”, the solution I propose here probably wouldn’t stand up to more robust test cases. But hey, it’s Free Code Camp, we are all learning, right? So to finalize this Missing Letter Algorithm I simply added an extra if statement. The final answer is shown below.


function fearNotLetter(str) {
  var result;
  var alpha = 'abcdefghijklmnopqrstuvwxyz';
  
  for (var i = 0; i < str.length; i++) {
    
    if (str[0] !== 'a') {
      result = undefined;
      
    } else if (str[i].indexOf(alpha[i]) < 0) {
      result = alpha[i];
      break;
    } 
   
  }

  return result;
}

fearNotLetter("abce");

If you are looking for a slightly more robust solution, then I most certainly recommend checking out this blog post on more advanced “Missing Letter Algorithm” solutions. If you have a cooler / better solution, please share it in the comments section below 🙂


The Web Developers Bootcamp.

A n00b friendly guide to becoming a pro!

If you’re feeling a bit overwhelmed and a little lost with all these crazy hard Free Code Camp exercises then this course is for you!

While I am oh so grateful for the opportunity to learn through Free Code Camp, I still find that learning to code alone and without proper guidance extremely exhausting. Which is why I’ve started to substitute my education with an incredible online resource called the Web Developers Bootcamp courtesy of Udemy.

This course has helped me brush off the cobwebs, unlearn my self-taught bad habits and gain a solid understanding of the true fundamentals of web development. It has helped me see why it is important to understand these JavaScript problems and how they apply to the real world.

This bootcamp will teach you how built beautiful sites with Bootstrap, add in functionality with JavaScript and write back end code with Node.js all in a series of short, sharp, no BS video modules. I seriously could not recommend it any higher. Check it out and let me know what you think 🙂

The Web Developers Bootcamp
The Web Developer Bootcamp.
The only course you need to learn web development – HTML, CSS, JS, Node, and More!

Sign up to receive up-to-date web tips, tricks and code hacks!