Suggestions Methods (SPOILERS for Missing Letters Exercise)

Suggestions Methods (SPOILERS for Missing Letters Exercise)
0

#1

So - I’ve found that the suggested methods on a given exercise are only used by me like 50% of the time - for instance the missing letters exercise links to charCode and charCodeAt - and while I recognize those - i saw no use of them in the exercise, as i immediate went to an ‘alphabet array’ solution (below)

function fearNotLetter(str) {
  var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
  var x = alphabet.indexOf(str[0]);

  for(i=0; i < str.length; i++  ) {
    if (str[i] == alphabet[x]) {
      x += 1;
    } else {
      return alphabet[x];
    }
  }
  return undefined;
}

So is my brain REALLY really weird, or are other people ignoring the suggested methods - or am I supposed to use the suggested methods (like set it up as an object where the key is the charCode for the letter, or vice versa?)


#2

I read the MDN docs for the suggested methods, but I often come up with some verbose and unwieldy solution instead.

With the example in question - I would personally use charCode and charCodeAt, but I can see why you might reach for a simple array instead. It makes intuitive sense, and the numbers align more sensibly with your expectations. Zero is a much nicer representation of the character a than 96 is!


#3

That’s just it - my mind immediately said 'go with the array and figure out how to start at the right place" then it’s just a simple loop test


#4

I don’t think you absolutely have to use the suggested methods. They are, after all, suggestions. They are really helpful links though, since they make you aware that such functions exist.

For that exercise...

…I’d most likely use the suggested functions. For each letter, I’ll get its char code, compare it to the next letter’s char code, then if the difference is not 1, return the missing letter using fromCharCode.


#5

I created a pretty simple solution that is really easy to understand, basically just using an if statement and a for loop. I wrote a blog about it here, if you wanna learn the process – http://jacklyons.me/missing-letter-algorithm/