8.5 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	title
| title | 
|---|
| Missing Letters | 
 Remember to use
 Remember to use Read-Search-Ask if you get stuck. Try to pair program  and write your own code
 and write your own code 
 Problem Explanation:
 Problem Explanation:
You will create a program that will find the missing letter from a string and return it. If there is no missing letter, the program should return undefined. There is currently no test case for the string missing more than one letter, but if there was one, recursion would be used. Also, the letters are always provided in order so there is no need to sort them.
Relevant Links
- String global object
- JS String Prototype CharCodeAt
- String.fromCharCode
 Hint: 1
 Hint: 1
You will need to convert from character to ASCII code using the two methods provided in the description.
try to solve the problem now
 Hint: 2
 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.
try to solve the problem now
 Hint: 3
 Hint: 3
You will need to figure out where the missing letter is, along with handling the case that there is not missing letter as it needs an specific return value.
try to solve the problem now
Spoiler Alert!
Solution ahead!
 Basic Code Solution:
 Basic Code Solution:
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 character + 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");
Code Explanation:
- This solutions makes use of a forloop.
- Code of encountered character is stored in code.
- It is checked if code of current character is the expected one (no characters are skipped) by using the logic - code of current character = code of first character + number of iterations.
- If a character is missing, the missing character is found and the final string is returned.
- undefinedis returned if there is no missing character in the string.
Relevant Links
- JS For Loops Explained
- String.length
 Intermediate Code Solution:
 Intermediate Code Solution:
// 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");
Code Explanation:
- 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 forandwhileloops.
- 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.
- If there are no missing characters, return undefined.
Relevant Links
 Simplified Advanced Code Solution:
 Simplified Advanced Code Solution:
function fearNotLetter(str) {
  for (let i = 1; i < str.length; ++i) {
    if (str.charCodeAt(i) - str.charCodeAt(i-1) > 1) {
      return String.fromCharCode(str.charCodeAt(i - 1) + 1);
    }
  }
}
Code Explanation:
- Loop over the string
- Check if the difference in char codes between adjacent characters in the string is more than 1 (check ASCII table)
- Return the missing character ( +1 from where the gap was detected)
 Advanced Code Solution:
 Advanced Code Solution:
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");
Code Explanation:
- A new string allChars is created.
- Create a regular expression notChars which selects everything except str.
- The forloop is used to add all the letters in the range to allChars.
- match()is used to strip off the str letters from the newly created string and it is returned.
- If there are no missing characters, return undefined.
Relevant Links
- JS Regex Resources
- JS Ternary
- JS String Prototype Match
- JS Array Prototype Join




