I've been dealing with an algorithm which takes a sequence of letters in alphabetic order, and if there is a character missing, it returns that character.
For example: fearNotLetter("abcdfg")
would return "e"
.
My questions are:
What is the logic behind this solution?
Why and how regex is used here?
How does the condition in the for loop work?
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;
}