Algorithm 7 - [JS] Find the missing letter

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!)

📌 Needs ) 빠진 글자를 찾아서 리턴하라.

📁 Sol ) for 반복문과 if 조건문 사용

function findMissingLetter(array) {
  let string = array.join("");
    for (let i = 0; i < string.length; i++) {
      if (string.charCodeAt(i + 1) - string.charCodeAt(i) != 1) {
      
      return String.fromCharCode(string.charCodeAt(i) + 1);
      }
    }
}

💡 Other ways to solve ) 변수의 value값이 길어서 사용 X. 그러나 split과 slice, find, includes 사용했다는 점에서 고려해볼 수 있음.

let findMissingLetter = (array) => {
  let alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
  let start = alphabet.indexOf(array[0]);
  return alphabet.slice(start, start + array.length).find(el => !array.includes(el));
};

좋은 웹페이지 즐겨찾기