-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-missing-letter.js
59 lines (48 loc) · 1.7 KB
/
find-missing-letter.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Find the Missing Letter
/*
Write a method that takes an array of consecutive (increasing) letters as input and that returns the missing letter in the array.
You will always get an valid array. And it will be always exactly one letter be missing. The length of the array will always be at least 2.
The array will always contain letters in only one case.
Example:
['a','b','c','d','f'] -> 'e'
['O','Q','R','S'] -> 'P'
(Use the English alphabet with 26 letters!)
*/
// input --> arr of increasing letters
// return --> missing letter of arr as a string
// arr will alwyas be valid and will always only be missing one letter
// the len of the array is always >= 2
// arr contains letters in only one case
// loop over input arr
// determine if nums are consec
// if not consec, return the charCode that would complete the sequence as string
// function findMissingLetter(array){
// let temp = 0;
// for(let i = 1; i < array.length; i++){
// if(array[i].charCodeAt() !== array[i - 1].charCodeAt()+1){
// temp += array[i - 1].charCodeAt()+1
// }
// }
// let result = String.fromCharCode(temp);
// return result;
// };
const findMissingLetter = array => {
let temp = 0;
for(let i = 1; i < array.length; i++){
if(array[i].charCodeAt() !== array[i - 1].charCodeAt()+1){
temp += array[i - 1].charCodeAt()+1;
};
};
const result = String.fromCharCode(temp);
return result;
}
// concise
const findMissingLetterClever = array => {
var i = array[0].charCodeAt();
array.map(x => x.charCodeAt() == i ? i++ : i);
return String.fromCharCode(i);
}
const x = ['a','b','c','d','f'];
const y = ['O','Q','R','S'];
console.log(findMissingLetter(x)) // "e"
console.log(findMissingLetter(y)) // "P"