Find missing letter in list of alphabets

前端 未结 16 1712
逝去的感伤
逝去的感伤 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条回答
  •  猫巷女王i
    2021-01-01 05:37

    I think that you wanted to say that if a string doesn't start with "a" than return "undefined". So here's my code:

     function fearNotLetter(str) {
       var alphabet = ("abcdefgheijklmnopqrstuvwxyz");
       var i = 0;
       var j = 0;
       while (i < alphabet.length && j < str.length) {
         if (alphabet.charAt(i) != str.charAt(j)) {
           i++;
           j++;
           if (alphabet.charAt(i - 1) == "a") {
             return "undefined";
           } else {
             return (alphabet.charAt(i - 1));
           }
         }
         i++;
         j++;
       }
     }
    
     alert(fearNotLetter('abce'));
    

    Here's the working JsFiddle.

    You wanted the code to return the missing letter so I used CharAt.
    You can make an array of letters and then search through it to see if it maches with letters from the string....

提交回复
热议问题