Find missing letter in list of alphabets

前端 未结 16 1714
逝去的感伤
逝去的感伤 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:34

    This is an even shorter answer thanks to RegExp() that allows you to create a Regular Expression on the fly and use match() to strip off the given letters from a generated String that has all the letters in the given range:

    function fearNotLetter(str) {
      var allChars = '';
      var notChars = new RegExp('[^'+str+']','g');
      for (var i=0;allChars[allChars.length-1] !== str[str.length-1] ;i++)
        allChars += String.fromCharCode(str[0].charCodeAt(0)+i);
      return allChars.match(notChars) ? allChars.match(notChars).join('') : undefined;
    
    }
    

提交回复
热议问题