Hey everyone! Today, we are going to work with strings, and get the next bonfire solved, Missing Letters. This exercise will have us do the following: Write a function, that given a string containing a letter range, returns the missing letter. If no letter is missing, it should return undefined.
For this task, we must use a couple string methods that you may be familiar with: charCodeAt() and fromCharCode(); charCodeAt will return the numerical character code for the character at the given position in a string, while fromCharCode will return a character when given a numerical value. Here’s a few examples:
var myString = 'John Doe'; myString.charCodeAt(0); //-> 74 myString.charCodeAt(2); //-> 104 myString.charCodeAt(7); //-> 101 String.fromCharCode(74); //-> J String.fromCharCode(104); //-> h String.fromCharCode(101); //-> e
Notice how fromCharCode is called as a method of the String object. Also, keep in mind that uppercase and lowercase characters have different character codes.
Let’s start working on a solution now that that’s been done with, how about some looping? We need to iterate over a string, so a good ol’ loop is sure to do the job! Since we want to return undefined if no character is missing, we are going to start by setting a new “missing” variable to undefined (or just declare it and do not set it to anything). We want to go letter by letter and return the missing character, when given a string ordered in alphabetical order, so every character code, should be equal to the previous characters code plus one. Let’s try implementing that:
function fearNotLetter(str) { var missing = undefined, prev = str.charCodeAt(0); // Character code for the first character. for (var i = 1; i < str.length; i++) { if (str.charCodeAt(i) !== prev + 1) { // Set missing to the missing letter. } else { prev++; } } return missing; }
As you can see, we first set both the missing and prev variables. The prev variable merely stores the previous characters code, and is updated every timne we check a new letter in the string.
When a character code is not equal to the previous characters code plus one, it means that a character is missing in between, and so, we must somehow get it into the missing variable. If no character is missing, we return the missing variable, which should still be undefined. Let’s implement the logic so we can get the missing character inside our missing variable (I’m starting to regret naming the variable that way…)
function fearNotLetter(str) { var missing = undefined, prev = str.charCodeAt(0); for (var i = 1; i < str.length; i++) { if (str.charCodeAt(i) !== prev + 1) { missing = String.fromCharCode(str.charCodeAt(i) - 1); return missing; } else { prev++; } } return missing; }
Notice a few things here. First, we are getting the actual character by using both, charCodeAt and fromCharCode in the same line. This way, we can get the missing character. We are also returning the missing variable as soon as we get a missing character, and don’t want to keep looking for more.
And that should get you set up! Remember, don’t just use this code and be done with it! Try using different methods and ways of iterating over the string, make it fast, make it original, make it yours!