JavaScript 中字串中丟失的字母


我們有一個長度為 m 的字串,其中包含英語字母的前 m 個字母,但不知何故,字串中缺少一個元素。因此,字串現在包含 m-1 個字母。

我們需要編寫一個函式,該函式帶入一個此類字串,並從字串中返回缺失的元素

因此,讓我們為該函式編寫程式碼 -

示例

程式碼如下 -

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

更新於: 2020-10-17

164 次瀏覽

啟動你的 職業

完成課程獲得認證

開始學習
廣告
© . All rights reserved.