我正在做Free Code Camp的篝火之一,我已经接近尾声,但最后一点我找不出来!缺少字母功能 - 为什么它返回undefined?
函数应该带一个字符串并返回丢失的字母(基于字母表a-z)。它工作正常,除了当缺少的字母是'我',它返回未定义。
我放了一个额外的if语句来检查当缺少的字母是'i'时,它符合其他if语句的标准(因此应该执行这些代码行)并匹配,所以我已经不知道为什么它会返回undefined。
function fearNotLetter(str) {
missingLetter = '';
charCode = 0;
for (i = 0; i < str.length -1 ; i++) {
charCode = str.charCodeAt(i);
if (str.charCodeAt(i + 1)-charCode == 2) {
missingLetter = str.charCodeAt(i)+1;
missingLetter = String.fromCharCode(missingLetter);
} else {
missingLetter = undefined;
}
}
console.log(missingLetter);
return missingLetter;
}
fearNotLetter("abcdefghjklmno");
真的很感谢所有帮助任何人都可以给。
在此先感谢。
感谢您的答案家伙 - 这是伟大的!对于我的学习,关于为什么只有某些字符串返回undefined而不是其他字符的想法? – StevenWalker