Find missing letter in list of alphabets

前端 未结 16 1685
逝去的感伤
逝去的感伤 2021-01-01 05:15

I am trying to solve the following issue:

Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefine

16条回答
  •  不思量自难忘°
    2021-01-01 05:31

    This will do what you're looking for:

    Hit run and check your console

    function missingLetter (str) {
        var alphabet = ("abcdefghijklmnopqrstuvwxyz");
        var first = alphabet.indexOf(str[0]);
        var strIndex = 0;
        var missing;
    
        for (var i = first ; i < str.length ; i++) {
            if (str[strIndex] === alphabet[i]) {
                strIndex++;
            } else {
                missing = alphabet[i];
            }
        }
        return missing;
    }
    
    console.log(missingLetter("abce"));
    console.log(missingLetter("bcd"));
    console.log(missingLetter("abcdefghjklmno"));
    console.log(missingLetter("yz"));

提交回复
热议问题