# freeCodeCamp Challenge Guide: Missing letters

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-missing-letters/16023>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 8:41pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-missing-letters/16023 "2016-06-30T20:41:28Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 8:41pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-missing-letters/16023/1 "2016-06-30T20:41:28Z")

</div>

# [Missing Letters](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters)

* * *

## 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)
- [JS String Prototype CharCodeAt](http://forum.freecodecamp.com/t/javascript-string-prototype-charcodeat/15933)
- [String.fromCharCode](http://forum.freecodecamp.org/t/javascript-string-fromcharcode-a-guide-to-the-fromcharcode-function/19102)

* * *

## Hints

### 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 the missing letter is, along with handling the case that there is not missing letter as it needs an specific return value.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function fearNotLetter(str) {
> for (let i = 0; i < str.length; i++) {
> /* code of current character */
> const charCode = str.charCodeAt(i);
> 
> /* if code of current character is not equal to first character + no of iteration
> then a letter was skipped */
> if (charCode !== str.charCodeAt(0) + i) {
> /* if current character skipped past a character find previous character and return */
> return String.fromCharCode(charCode - 1);
> }
> }
> return undefined;
> }
> 
> // test here
> fearNotLetter("abce");
> 
> ```
> 
> #### Code Explanation
> 
> - This solutions makes use of a `for` loop.
> - 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.
> - `undefined` is returned if there is no missing character in the string.
> 
> #### Relevant Links
> 
> - [JS For Loops Explained](http://forum.freecodecamp.com/t/javascript-for-loop/14666s-Explained)
> - [String.length](http://forum.freecodecamp.org/t/javascript-string-length-javascript-length-explained-with-examples/19101)

> **Solution 2 (Click to Show/Hide)**
>
> ```javascript
> function fearNotLetter(str) {
> let currCharCode = str.charCodeAt(0);
> let missing = undefined;
> 
> str
> .split("")
> .forEach(letter => {
> if (letter.charCodeAt(0) === currCharCode) {
> currCharCode++;
> } else {
> missing = String.fromCharCode(currCharCode);
> }
> });
> 
> 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 letter we may find.
> - We turn the string to an array in order to forEach through it instead of using `for` and `while` loops.
> - As we `forEach` through 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 `missing` variable, which will be returned after the map is finished.
> - If there are no missing characters, return `undefined`.
> 
> #### Relevant Links
> 
> - [JS String Prototype Split](http://forum.freecodecamp.com/t/javascript-string-prototype-split/15944)

> **Solution 3 (Click to Show/Hide)**
>
> ```javascript
> 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)
> 
> ```javascript
> 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 `for` loop 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](http://forum.freecodecamp.org/t/regular-expressions-the-ultimate-list-of-learning-resources/15931)
> - [JS Ternary](http://forum.freecodecamp.com/t/javascript-ternary-operator/15973)
> - [JS String Prototype Match](http://forum.freecodecamp.com/t/javascript-string-prototype-match/15941)
> - [JS Array Prototype Join](http://forum.freecodecamp.com/t/javascript-array-prototype-join/14292)

---

_[View the full topic](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-missing-letters/16023)._
