Missing letters - Solution 4 explanation

I haven’t coded this, but I find it super clever, building a string containing all the different letters until the missing one and then finding a correspondence with match method.

However I don’t get str[0].charCodeAt(0), that’s a typo, right? Should be str.charCodeAt(0), right? Not sure why it’s not breaking the code actually.

Your code so far

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;
}

// test here
fearNotLetter("abce");

Challenge: Missing letters

Link to the challenge:

They both essentially do the same. It can be looked at as:

'abce'.charCodeAt(0)
'a'.charCodeAt(0)

Because charCodeAt() method always uses here 0 as argument it always will be char code of that first character.

1 Like

I’ve edited your post.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Duly noted @ilenia I’ll be careful about that in the future :+1: