找出字符串中缺失的字母 - 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP