Problem
- Write a method that takes an array of consecutive(increasing) letters as input and that returns the missing letter in the array.
- 연속적인(증가하는) 문자 배열을 입력(input)으로 사용한다.
- 배열에서 누락된 문자를 반환한다.
- 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.
Solution 01
function findMissingLetter(arr) {
let first = arr[0].charCodeAt(0);
for (let i = 1; i < arr.length; i++) {
if (first + i !== arr[i].charCodeAt(0)) {
return String.fromCharCode(first + i);
}
}
}
findMissingLetter(['a', 'b', 'd']); // c
findMissingLetter(['A', 'B', 'D']); // C
findMissingLetter(['A', 'C', 'D']); // B
Solution 02
function findMissingLetter(arr) {
const alphabetArray = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const first = alphabetArray.indexOf(arr[0]);
return alphabetArray.slice(first, first + arr.length).find(i => !arr.includes(i));
}
findMissingLetter(['a', 'b', 'd']); // c
findMissingLetter(['A', 'B', 'D']); // C
findMissingLetter(['A', 'C', 'D']); // B