-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMissingLetter.js
More file actions
64 lines (54 loc) · 2.02 KB
/
FindMissingLetter.js
File metadata and controls
64 lines (54 loc) · 2.02 KB
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
60
61
62
63
64
//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'
//["a","b","c","d","f"] -> "e"
//["O","Q","R","S"] -> "P"
//(Use the English alphabet with 26 letters!)
//Have fun coding it and please don't forget to vote and rank this kata! :-)
//I have also created other katas. Take a look if you enjoyed this kata!
// My Solution
function findMissingLetter(array)
{ const alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
const upperalph = alphabets.map(alphabet =>(alphabet.toUpperCase()))
for (let x = 0; x<alphabets.length;x++) {
if (alphabets[x]== array[0])
var first = x
if (upperalph[x] == array[0])
var firstUpper = x
}
var slice = []
for (let x = first; x<(first+array.length); x++) {
slice.push(alphabets[x])}
if (first !== undefined) {
for (let x in slice) {
if (array.indexOf(slice[parseInt(x)])==-1) {
return slice[parseInt(x)]
}
}
}
for (let x = firstUpper; x<(firstUpper+array.length); x++) {
slice.push(upperalph[x])}
if (firstUpper !== undefined) {
for (let x in slice) {
if (array.indexOf(slice[parseInt(x)])==-1) {
return slice[parseInt(x)]
}
}
}
}
// Better
function findMissingLetter(array)
{
var i=array[0].charCodeAt();
array.map(x=> x.charCodeAt()==i?i++:i);
return String.fromCharCode(i);
}
// Other
const findMissingLetter = (array) => {
const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const start = alphabet.indexOf(array[0]);
return alphabet.slice(start, start + array.length).find(el => !array.includes(el));
};