Replace with Alphabet Position

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!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CSS Flexbox Fundamentals Visual Guide

Bringing TypeScript types at runtime with TypeOnly

Intro to JavaScript…

Lesson 1 : Basics of Components

How to Apply Nested Transitions in Vue

ToDo web-app using Angular: A nice approach towards CRUD.

Angular 8 Crud Todo Application Tutorial

Exponent SDK v13.0.0 is now available

Angualr with ASP.NET Core Tutorial

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marcos Lipic

Marcos Lipic

More from Medium

What does the phrase “to eat crow” mean and Where does it come from?

Preview of the week starting 18Apr2022 — news & calendars, S&P500 and stock of interest ~ Micron

Apr 2022 earnings

25 YEARS LATER, THE JOCKS AND COOLS KIDS STILL DON’T HAVE TO PLAY BY THE RULES

The Yankees lost their first game of the season to Toronto to fall to 2–2