W3Cschool中级脚本算法(8.字符串查询补充算法挑战)

字符串查询补充算法挑战


问题:

从传递进来的字母序列中找到缺失的字母并返回它。

如果所有字母都在序列中,返回 undefined。


要求:

fearNotLetter("abce") 应该返回 "d"。

fearNotLetter("abcdefghjklmno") 应该返回 "i"。

fearNotLetter("bcd") 应该返回 undefined。

fearNotLetter("yz") 应该返回 undefined。


问题答案:

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");

题目链接:

https://www.w3cschool.cn/codecamp/missing-letters.html

猜你喜欢

转载自blog.csdn.net/qq_42044073/article/details/82663664