挑战:
参考:字符串查询补充算法挑战
从传递进来的字母序列中找到缺失的字母并返回它。
如果所有字母都在序列中,返回 undefined。
例如
fearNotLetter("abce") 应该返回 "d"。
fearNotLetter("abcdefghjklmno") 应该返回 "i"。
fearNotLetter("bcd") 应该返回 undefined。
fearNotLetter("yz") 应该返回 undefined。
答案:
方法 | 描述 |
---|---|
charCodeAt() | 返回在指定的位置的字符的 Unicode 编码。 |
fromCharCode() | 从字符编码创建一个字符串。 |
function fearNotLetter(str) {
var sub;
for (var index = 0; index < str.length - 1; index++) {
sub = str.charCodeAt(index + 1) - str.charCodeAt(index);
if (sub == 1) {
if (index == str.length) {
return undefined;
}
}else {
return String.fromCharCode(str.charCodeAt(index) + 1);
}
}
}
fearNotLetter("abce");
运行结果:
d
在线测试: