Missing Letters

From the FreeCodeCamp intermediate algorithms here:

Find the missing letter in the passed letter range and return it.

If all letters are present in a range, return undefined.

FreeCodeCamp – Missing Letters

There’s not too much to say about this one. One thing which I can note is that JavaScript, as a language, is extremely helpful.

In this challenge you need to iterate through the given string, and essentially check that each item is in alphabet order. If an item is not in alphabet order it means that a letter has been missed out (that’s obviously only specifically in this challenge – and you may have to do other checks IRL). So once you’ve found that there is a missing letter, you can ‘ascii-ertain’ what that letter is and return it (…see what I did there? Not funny, I know.)

The ASCII table shows how computers, and programming languages, interpret characters – in decimal format.

Coupled with the helpful JavaScript String methods: String.fromCharCode() and charCodeAt() you can return the missing letter with some simple maths!

My solution:

function fearNotLetter(str) {
  for (let i = 1; i < str.length; i++) {
    if (str.charCodeAt(i) - str.charCodeAt(i-1) > 1) {
      return String.fromCharCode(str.charCodeAt(i - 1) + 1);
    }
  }
}

fearNotLetter("abce");

The FreeCodeCamp forum has some extra information, and some helpful links about the String global object and it’s methods.

Published by Zachary

Learning to code with free code camp - zed.code.blog. Reading and writing at alwaysbooks.co.uk.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: