-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.missing.js
More file actions
38 lines (35 loc) · 976 Bytes
/
string.missing.js
File metadata and controls
38 lines (35 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* @title: find missing letter
* @description: find missign letter from alphabet
* @author: Thorsten Kober
* @email: [email protected]
*/
function findMissingLetter(str) {
let previous = str.charCodeAt(0);
let current = str.charCodeAt(0);
for (let i = 1; i < str.length; i++) {
previous = current;
current = str.charCodeAt(i);
if (current !== previous + 1) {
return String.fromCharCode(previous + 1);
}
}
return null;
}
function findMissingLetter2(str) {
for (let i = 1; i < str.length; i++) {
const previous = str.charCodeAt(i - 1);
const current = str.charCodeAt(i);
if (current - previous > 1) {
return String.fromCharCode(previous + 1);
}
}
return null;
}
// npx jest algorithms/string/string.missing.js
test('findMissingLetter()', () => {
expect(findMissingLetter('abce')).toEqual('d');
});
test('findMissingLetter2()', () => {
expect(findMissingLetter2('abcdefghjklmno')).toEqual('i');
});