DNA Pairing - JavaScript Solution & Walkthrough
(08/21) Learn how to solve coding challenges using FreeCodeCamp's curriculum.
08/21 DNA Pairing
The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.
Base pairs are a pair of AT and CG. Match the missing element to the provided character.
Return the provided character as the first element in each array.
For example, for the input
GCG
, return[["G", "C"], ["C","G"], ["G", "C"]]
The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.
function pairElement(str) {
return str;
}
pairElement("GCG");
Credit: FreeCodeCamp.org
Understanding the Challenge
In the DNA Pairing challenge, we are expected to use base pairs provide the corresponding pairing letter for each given letter. The challenge description informs us that base pairs are a pair of AT and CG. This means that A always goes with T. And C always goes with G.
Also, we are told that the provided character should always be the first letter in the pairing. For example, If A is given, then the pair to be return should be [["A", "T"]]
. However,
if T is the given letter, then the pair to be returned will be [["T", "A"]]. The same goes for "C" and "G".
Note how the return value is a 2d array (an array with sub-arrays). This is part of the requirements for this challenge. Okay, now let's penned down some pseudocode before we dive into it.
Pseudocode
Given a string of characters,
Create an empty array to hold all pairings (finalArray)
Check the provided letters
if a letter is "A"
pair it with "T" in an array
push the array into finalArray
if a letter is "T"
pair it with "A" in an array
push the array into finalArray
if a letter is "C"
pair it with "G" in an array
push the array into finalArray
if a letter is "G"
pair it with "C" in an array
push the array into finalArray
Return finalArray
Solving the Challenge
Considering our pseudocode above, we can solve this challenge using the if
and else if
statements. However, this is a good case where using
the switch
statement will be more appropriate to use. Using the switch
statement in such a case will help you write a more readable code.
If you're not familiar with the switch
statement, I will encourage you to first solve this problem with the if
and else if
statements. Then
you learn about the switch
statement. I've provided a link to a tutorial in the Useful Links section below.
When you've learned the syntax of the switch
statement, come back to this challenge and see how solving it with switch
differs.
Okay, now let's get into the solution.
First, let's create a variable that would hold the final array to be returned. We will call it finalArray
.
let finalArray = [];
The next thing we will done is to use a for loop
. We want to loop through all the given letters. Then, for every given letter, we'll use the switch
statement to check what letter it is and determine how it will be paired.
for (let i = 0; i < str.length; i++) {
switch(str[i]) {
}
}
To use the switch
, we write the keyword switch
followed by parenthesis. Inside the parenthesis, we state what will be checking. In this case, what we're going to check for every iteration of the loop is the letter whether it is "A", "T", "G" or "C".
Then, we use the key word case
for the individual cases and determine what should happened in each of the cases.
case 'A':
finalArray.push(["A", "T"])
break;
The above code snippet shows that when the given letter is A
, our function should push a sub-array of ["A", "T"]
into the finalArray
. If the case is fulfilled, we break out of the switch and move to the next iteration of the loop. We'll do the same thing for all the available cases (or letters in this challenge.)
case 'T':
finalArray.push(["T", "A"])
break;
case 'C':
finalArray.push(["C", "G"])
break;
case 'G':
finalArray.push(["G", "C"])
break;
default:
return "value cannot be paired";
It is best practice to always have a default case in your switch
to cater for unexpected cases. For example, assuming a user provides an input of "B". In our function the letter "B" cannot be paired. So your switch
statement should be able to inform your users of that.
Now, we return our finalArray
and our function is complete.
return finalArray;
Final Solution
function pairElement(str) {
let finalArray = [];
for (let i=0; i<str.length; i++) {
switch(str[i]) {
case 'A':
finalArray.push(["A", "T"])
break;
case 'T':
finalArray.push(["T", "A"])
break;
case 'C':
finalArray.push(["C", "G"])
break;
case 'G':
finalArray.push(["G", "C"])
break;
default:
return 'value not found';
}
}
return finalArray;
}
pairElement("GCG");
Congratulations!
You just cracked the eigth challenge in this series.
Cheers and happy coding!