Bonfire Missing letters - GJSmith3rd/FreeCodeCamp-BootCamp GitHub Wiki
Contact me
Gilbert Joseph Smith III
Github | FreeCodeCamp | CodePen | LinkedIn | Blog/Site | E-Mail
Details
- Difficulty: 2/5
Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return undefined.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Useful Links
Problem Script:
function fearNotLetter(str) {
return str;
}
fearNotLetter('abce');
Problem Explanation:
- You will create a program that will find the missing letter from a string and add it. If there is not missing letter it will return undefined. There is currently no test case for it missing more than one letter, but if anything recursion can be implemented or a second or more calls to the same function as needed. Also the letters are always provided in order so there is no need to sort them.
Hint: 1
- You will need to convert from character to ASCII code using the two methods provided in the description.
Hint: 2
- You will have to check for the difference in ASCII code as they are in order. Using a chart would be very helpful.
Hint: 3
- You will need to figure out where to insert the letter and how to do it, along with handling the case that there is not missing letter as it needs an specific return value.
Spoiler Alert!
Solution ahead!
Code Solution:
function fearNotLetter(str) {
// Create our variables.
var firstCharacter = str.charCodeAt(0);
var valueToReturn = '';
var secondCharacter = '';
// Function to find the missing letters
var addCharacters = function(a, b) {
while (a - 1 > b) {
b++;
valueToReturn += String.fromCharCode(b);
}
return valueToReturn;
};
// Check if letters are missing in between.
for (var index = 1; index < str.length; index++) {
secondCharacter = str.charCodeAt(index);
// Check if the diference in code is greater than one.
if (secondCharacter - firstCharacter > 1) {
// Call function to missing letters
addCharacters(secondCharacter, firstCharacter);
}
// Switch positions
firstCharacter = str.charCodeAt(index);
}
// Check whether to return undefined or the missing letters.
if (valueToReturn === '')
return undefined;
else
return valueToReturn;
}
Code Explanation:
- Read comments in code.