-
Notifications
You must be signed in to change notification settings - Fork 0
/
Findthemissingletter.js
45 lines (35 loc) · 1.48 KB
/
Findthemissingletter.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
// 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!)
// 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!
function findMissingLetter(array){
let alphabet = ['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' , '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']
let j = alphabet.indexOf(array[0])
for(let i = 0; i < array.length; i++){
if(array[i] !== alphabet[j]){
return alphabet[j]
}
j++
}
}
//different approach
function findMissingLetter(array)
{
var i=array[0].charCodeAt();
array.map(x=> x.charCodeAt()==i?i++:i);
return String.fromCharCode(i);
}
function findMissingLetter(letters) {
for (var i = 0; i < letters.length; i++) {
if (letters[i].charCodeAt() + 1 !== letters[i + 1].charCodeAt()) {
return String.fromCharCode(letters[i].charCodeAt() + 1);
}
}
}