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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP