找出字串中缺失的字母 - JavaScript


我們有一個長度為 m 的字串,它包含英語字母的前 m 個字母,但不知怎的,一個元素從字串中丟失了。所以現在,字串包含:

m-1 letters

我們需要編寫一個函式,該函式接收這樣的一個字串,並返回字串中缺失的元素。

例如

以下是程式碼 -

const str = "acdghfbekj";
const missingCharacter = str => {
   // to make the function more consistent
   const s = str.toLowerCase();
   for(let i = 97; ; i++){
      if(s.includes(String.fromCharCode(i))){
         continue;
      };
      return String.fromCharCode(i);
   };
   return false;
};
console.log(missingCharacter(str));

輸出

以下是控制檯中的輸出 -

i

更新於: 16-Sep-2020

628 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告