FCC Intermediate Algorithm Scripting Challenge – ‘DNA Pairing’


Today, we’ll cover freeCodeCamp’s Intermediate Scripting Challenge ‘DNA Pairing‘.  The challenge here is to write a function that accepts a string of characters and returns a 2D array of matched base pairs.

If you’re not caught up with your molecular biology, here’s a quick lesson…

Molecular Bio 101 (mostly not needed to solve algorithm)

By Zephyris at the English language Wikipedia, CC BY-SA 3.0
By Zephyris at the English language Wikipedia, CC BY-SA 3.0

DNA is the double stranded nucleic acid that carries your genetic information.  Each single strand is composed of units called nucleotides, of which, there are four – cytosine (C), guanine (G), adenine (A) and thymine (T).  In double stranded DNA, these nucleotides bond in specific base pairs, A with T and C with G.

DNA stores all kinds of biological information and make up our chromosomes, which, in turn, make you who you are…  Enough bio, let’s get back to solving the algorithm….

Bio Class Dismissed…

As we get into the intermediate and advanced algorithm challenges, each challenge will have multiple (even many) approaches to a solution.  My plan with Crooked Code is to explain the solution that I used, then search for solutions used by others in order to illustrate the various ways an algorithm can be written.

This is what freeCodeCamp gives us as a starting point for ‘DNA Pairing’:

The instructions provided for this challenge tell us that we’ll be getting a string as an argument and we must return a 2D array that matches up each element (letter) with it’s base pair counterpart.

Let’s get started working on a solution…

I started by ‘splitting’ the string argument into an array using string.split() method.  I covered string.split() in depth here.   I also created some variables that we’ll use later in the function.  My ‘pairElement’ function now looks like:

We now must scroll through ‘singleArr’ and pair up each element with it’s matching pair.  We can use a for loop to scroll through the array, but how can we match up the pairs?

A bunch of if/else statements would work but a switch statement is a better option here.  A switch statement will evaluate a given expression, then transfer control to the case that matches the outcome of the expression.

I’ve included the syntax of a switch statement written by mozilla in order to make the explanation easier.

In our case, the expression will be just a letter, as will each case value.  We can then write a statement to be executed for each individual case.

To sum up the logic for our algorithm…

  • split the given string into an array
  • loop through array and match each element with it’s pair
  • ‘push’ each new pair into 2D array
  • return 2D array

At this point, we’ve covered everything needed to solve the ‘pairElement’ function.  If you’re working through the freeCodeCamp curriculum you may want to go try to solve it on your own, otherwise, read on…


Here’s the solution to the ‘DNA Pairing’ challenge that I came up with:

The freeCodeCamp wiki page (written by @Rafase282 and @sabahang) provides 2 solutions to this algorithm challenge.  The first, ‘basic’, solution is very similar to mine, using a switch statement, so I won’t include it for the sake of redundancy.

The second, ‘intermediate’, solution uses an object, and we haven’t really covered objects in much detail here at Crooked Code.  I’ll include the code below so we can see a second, very different solution, but won’t provide an explanation, as objects are far too big of a topic to cover at the end of this post.

You can see an explanation to this solution at the wiki page.

That’s it for now, hopefully this helped straighten out the code…


Learning to Program? We’ll help straighten out the code…

Learning to code, becoming a self-taught programmer, can be a daunting task when you are just beginning the process. The good news is there are tons of resources out there to help.

I started my learn to code journey earlier this year and have spent many hours sorting through all the resources out there to find the good ones. In some cases, however, even the good resources include jargon that might be confusing and difficult to understand for the beginner. I’ve created crookedCode in an attempt to break through that jargon and help others along their journey.

What makes me qualified to write about something I’m only in the process of learning? Great question… Rather than trying to justify what makes me qualified to cover these subjects, let me give the two reasons why I’ve created this blog:

The first reason, as I mentioned above, is that I’ve spent many hours trying to find good resources to help along the way. I hope that sharing what I’ve learned, and where to look for help, will enable others spend more time learning to code (and less time looking for how to learn).

The second reason, and this is a bit more selfish, is that going back and writing posts about challenges and projects that I’ve already completed will help solidify the concepts in my head. I find that the best way to truly understand a concept is to try to teach it to someone else. So that’s what I plan to do.

I’m currently working my way through freeCodeCamp. If you’re interested in learning to program, especially web development, I highly recommend checking them out. It’s a completely free program, with a very active and supportive community, and if you need another reason, they’re also doing some social good through their work with nonprofits.

We’ll start by working through examples of the freeCodeCamp curriculum (both algorithm challenges and development projects) and move on from there as we get more proficient. That said, we’ll be focusing on the logic behind the algorithms and we’ll be coding in Javascript, HTML and CSS.

I’ve learned quickly that there are usually multiple ways to solve coding problems. So, being new to this, I’m sure that I will not always provide the most efficient solution or most elegantly written code, so I welcome the feedback and suggestions. Please keep it positive though, there are plenty of other sites out there where you can berate others and get your aggressions out, please treat this as a positive learning environment.