FCC-學習筆記 Missing letters 1>最近在學習和練習FCC的題目。這個真的比較的好,推薦給大家。 2>中文版的地址:https://www.freecodecamp.cn/;英文版的地址:https://www.freecodecamp.org 3>這次寫關於一個JS的問題,名為M ...
FCC-學習筆記 Missing letters
1>最近在學習和練習FCC的題目。這個真的比較的好,推薦給大家。
2>中文版的地址:https://www.freecodecamp.cn/;英文版的地址:https://www.freecodecamp.org
3>這次寫關於一個JS的問題,名為Missing letters.
規則要求如下:
從傳遞進來的字母序列中找到缺失的字母並返回它。
如果所有字母都在序列中,返回 undefined
4>我寫的代碼實現如下:
function fearNotLetter(str) { var result; for(var i=0;i<str.length-1;i++){ if(str[i+1].charCodeAt()-str[i].charCodeAt()>1){ result=String.fromCharCode(str[i+1].charCodeAt()-1); break; }else if(str[i+1].charCodeAt()-str[i].charCodeAt()==1){ result=undefined; } } return result; } //測試過程 fearNotLetter("abce"); fearNotLetter("abcdefghjklmno"); fearNotLetter("bcd"); fearNotLetter("yz");
5>寫的不好還需要改進,期待大家的指出,共同進步!