forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 1
Algorithm Missing Letters
Rafael J. Rodriguez edited this page Jun 13, 2016
·
4 revisions
- 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.
- You will need to convert from character to ASCII code using the two methods provided in the description.
- You will have to check for the difference in ASCII code as they are in order. Using a chart would be very helpful.
- 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.
Solution ahead!
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;
}
// test here
fearNotLetter("abce");🚀 Run Code
function fearNotLetter(str) {
for(var i = 0; i < str.length; i++) {
/* code of current character */
var code = str.charCodeAt(i);
/* if code of current character is not equal to first charcter + no of iteration
hence character has been escaped*/
if ( code !== str.charCodeAt(0) + i) {
/* if current character has escaped one character find previous char and return*/
return String.fromCharCode(code-1);
}
}
return undefined;
}
// test here
fearNotLetter("abce");🚀 Run Code
// Adding this solution for the sake of avoiding using 'for' and 'while' loops.
// See the explanation for reference as to why. It's worth the effort.
function fearNotLetter(str) {
var compare = str.charCodeAt(0), missing;
str.split('').map(function(letter,index){
if (str.charCodeAt(index) == compare) { ++compare; }
else { missing = String.fromCharCode(compare); }
});
return missing;
}
// test here
fearNotLetter("abce");🚀 Run Code
function fearNotLetter(str) {
var strArr = str.split('');
var missingChars = [], i = 0;
var nextChar = String.fromCharCode(strArr[i].charCodeAt(0)+1);
while (i<strArr.length - 1) {
if (nextChar !== strArr[i+1]){
missingChars.push(nextChar);
nextChar = String.fromCharCode(nextChar.charCodeAt(0)+1);
} else {
i++;
nextChar = String.fromCharCode(strArr[i].charCodeAt(0)+1);
}
}
return missingChars.join('') === '' ? undefined : missingChars.join('') ;
}
// test here
fearNotLetter("abce");🚀 Run Code
function fearNotLetter(str) {
var allChars = '';
var notChars = new RegExp('[^'+str+']','g');
for (var i=0;allChars[allChars.length-1] !== str[str.length-1] ;i++)
allChars += String.fromCharCode(str[0].charCodeAt(0)+i);
return allChars.match(notChars) ? allChars.match(notChars).join('') : undefined;
}
// test here
fearNotLetter("abce");🚀 Run Code
- Read comments in code.
- First we define variables to store the character code for the first letter in the string, and to store whatever missing letters we may find.
- We turn the string to an array in order to map through it instead of using those nasty
forandwhiles(See this article for refence as to why. And give it a serious try. Really... you'll thank me for it. - (If you're not sure how to
map, you can review [Challenge: Iterate over arrays with map]](http://www.freecodecamp.com/challenges/iterate-over-arrays-with-map) and MDN's reference). - As we
mapthrough our letters' character codes, we go comparing with the one that should be in that position. - If the current letter matches, we move the comparison variable to its next position so we can compare on the next cycle.
- if not, the missing letter will be assigned to the
missingvariable, which will be returned after the map is finished.
- Increase loop index only when you have found all the missing letters between current and next letter
- Every time you find a missing letter push it to
missingchars
- Create a new String that consists all the letters in the range
- Create a Regular Expression for anything except
str - Use
match()to strip off thestrletters from your newly created String
If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @rohitnwn @sabahang @Hallaathrad for your help with Algorithm: Missing Letters
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links