Skip to content

Commit 4489b96

Browse files
committed
Complete missing letters algorithm
1 parent 7857779 commit 4489b96

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ From the Freecodecamp Javascript Certification Intermediate Algorithms module
1212
- [Pig Latin](#pig-latin)
1313
- [Search and Replace](#search-and-replace)
1414
- [DNA Pairing](#dna-pairing)
15+
- [Missing Letters](#missing-letters)
1516

1617
#### Sum All Numbers In a Range
1718

@@ -174,3 +175,26 @@ export function pairElements(str) {
174175
return pairs;
175176
}
176177
```
178+
179+
#### Missing Letters
180+
181+
Find the missing letter in the passed letter range and return it.
182+
183+
If all letters are present in the range, return undefined.
184+
185+
```javascript
186+
export function fearNotLetter(str) {
187+
if ((str.charCodeAt(str.length - 1) - str.charCodeAt(0)) + 1 === str.length) {
188+
return undefined;
189+
}
190+
191+
for (let i = 0; i < str.length - 1; i++) {
192+
const currentCharCode = str.charCodeAt(i);
193+
if (currentCharCode + 1 !== str.charCodeAt(i + 1)) {
194+
return String.fromCharCode(currentCharCode + 1);
195+
}
196+
}
197+
198+
return undefined;
199+
}
200+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function fearNotLetter(str) {
2+
if ((str.charCodeAt(str.length - 1) - str.charCodeAt(0)) + 1 === str.length) {
3+
return undefined;
4+
}
5+
6+
for (let i = 0; i < str.length - 1; i++) {
7+
const currentCharCode = str.charCodeAt(i);
8+
if (currentCharCode + 1 !== str.charCodeAt(i + 1)) {
9+
return String.fromCharCode(currentCharCode + 1);
10+
}
11+
}
12+
13+
return undefined;
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fearNotLetter } from "../../src/fcc-intermediate-algorithms/missing_letters";
2+
3+
test('should find missing character', () => {
4+
expect(fearNotLetter("abcdefghjklmno")).toBe("i");
5+
expect(fearNotLetter("abcdefghijklmnopqrstuvwxyz")).toBe(undefined);
6+
});

0 commit comments

Comments
 (0)