查找字符串中缺失的字母 - JavaScript

javascriptweb developmentfront end technologyobject oriented programming更新于 2025/12/7 3:24:17

我们有一个长度为 m 的字符串,其中包含英文字母表的前 m 个字母,但不知何故,字符串中缺少一个元素。所以,现在字符串包含,

m-1 个字母

我们需要编写一个函数,接受一个这样的字符串并返回字符串中缺失的元素

示例

以下是代码 −

const str = "acdghfbekj";
const missingCharacter = str => {
   // 使功能更加一致
   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

相关文章


有用资源