Missing Letters - JavaScript Solution & Walkthrough
(09/21) Learn how to solve coding challenges using FreeCodeCamp's curriculum.
09/21 Missing Letters
Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return
undefined
.
function fearNotLetter(str) {
return str;
}
fearNotLetter("abce");
Credit: FreeCodeCamp.org
Understanding the Challenge
In the missing letters challenge, we are presented with a function that takes a range of letters. Here, a range of letters refers to a group of letters with the first letter followed by the the letter that comes directly after it in the alphabets. The second is also followed by the letter that comes directly after it and so on and so forth.
For example abc
is a range of letters. This is because in the alphabets, a
comes directly before b
also which comes directly before c
Another example is wxyz
. w
comes directly before x
which comes before y
which comes before z
Our task in this challenge is that we will be given a range. However, there might be a letter missing in the range.
If that is the case, then we should complete the function such that it returns the missing letter. If there is not
missing letter in the range, then our function should return undefined
;
For example abce
should return d
. This is because there is a missing letter d
which should come in between c
and e
.
Another example is mnop
should undefined
because there is no missing letter in the given range.
Pseudocode
Given a range of letters
loop through the range
for every iteration of the loop
Compare the character code of next letter and current letter
if it is more than one, it indicates a missing letter
return the letter that comes after the current letter
if there is no difference of more than one, it means no missing letter
return undefined
Solving the Challenge
First, we need a for loop
to loop through the letters in the given range.
For this challenge, we will need the ASCII Table
This table contains letters, numbers, other characters and symbols. And each one of these is given a code in the ASCII table.
For example a
has a code of 97, b
is 98, c
is 99 and so on.
Thus, for our function to determine where there's a missing number in the range, we can use the code to find out.
For every iteration of the loop, we will check the difference between the code of the next character in the range and the code of the currect character. If the difference is more than one, it means there's a missing letter.
To get the code of a letter, we use .charCodeAt()
for (let i = 0; i < str.length; i++) {
if (str.charCodeAt(i+1) - str.charCodeAt(i) > 1) {
}
}
If the difference is more than one, then we know there's a missing letter. We can find the character code of that missing letter by adding 1 to the character code of the current letter.
let's save the code of the missing letter in a variable called missingIndex
Once we have the character code of the missing letter, we can use String.fromCharCode()
to find the letter itself which is what we shall return.
const missingIndex = str.charCodeAt(i) + 1;
return String.fromCharCode(missingIndex);
However, if after looping through the provided range, we do not find any instance where the character code of the next letter minus the character code of the current letter is greater than one(1),
then it means there's no missing letter. In that case, we just return undefined
and our function is complete!
Final Solution
function fearNotLetter(str) {
for (let i = 0; i < str.length; i++) {
if (str.charCodeAt(i+1) - str.charCodeAt(i) > 1) {
const missingIndex = str.charCodeAt(i) + 1;
return String.fromCharCode(missingIndex);
}
}
return undefined
}
fearNotLetter("abce"); // "d"
Congratulations!
You just cracked the ninth challenge in this series.
Cheers and happy coding!