Replace with Alphabet Position

Marcos Lipic
4 min readJan 2, 2021


Photo by Gaelle Marcel on Unsplash

Codewars challenges are a great way to sharpen your coding skills. It’s an opportunity to work on analytical problem solving, as well as becoming more familiar with the coding language you choose to work with. Today I’m going to walk through my solution to the Replace with Alphabet Position kata that I completed with JavaScript.

The task is to, given a string, replace every letter with its position in the alphabet. So “A” should be converted to 1, “B” to 2, and so on. If anything in the text isn’t a letter, ignore it and don’t return it.

Okay, so given these instructions, how should we approach this problem? Before we start writing our first line of code, let’s think about a general plan for tackling this challenge. One way to attack this could be to create a JavaScript object where each name-value pair groups together a letter with the correct number. Then for each character in the given string, we could use the object to convert the letters to numbers.

Fortunately, there’s another way, as is often the case when programming, that does not require building such a large object. We can use the JavaScript method: charCodeAt(). This method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index. Great! So now that we have a mechanism for converting letters to numbers, we need to ensure that the numbers being produced are the correct numbers. Let’s test what numbers charCodeAt() gives us. We can output values to the console to get a sense of what is going on here. Let’s use “A”, “Z”, “a”, and “z” and see what output we get using charCodeAt().

So we can see that this method converts “A” to 65, “Z” to 90, “a” to 97, and “z” to 122. If we subtract 64 from 65 we are left with 1, which is the value that “A” corresponds to. Therefore, if we convert all the letters to capital letter, use charCodeAt(), and subtract 64 from all the integers between 64 and 91, we will have achieved our goal of converting all the letters to their respective alphabet position.

Okay. Let’s make this happen! First we need to create the function.

Then all the text should be converted to capital letters.

Now we are ready to use charCodeAt(). The string of uppercase letters will be converted into an array that we can map over, using charCodeAt() for each letter.

At this point, a new array with the correct numerical values can be created. It will be made using the filter() method. For every number in the array that is greater than 64 and less than 91, place into the new array that number.

Afterwards, a third array with the correct numerical values is made by mapping over the previous array and subtracting 64 from each number in the array.

Lastly, we convert the final array back to a string and return the string.

All together, the function in its entirety can be seen below.

There you have it! This is a great coding exercise that exposes you to various JavaScript methods, some very common ones like filter() and map(), and less frequently used ones like toUpperCase() and charCodeAt(). Most importantly, it invites you to think carefully, using you analytical problem solving skills. Hopefully, this helps you in coding adventures!