# FCC Bonfire Series 124: DNA Pairing

Hey there! After a well spent vacation time, I’m back with a new bonfire solution. This time, we are going to be working through the FCC DNA Pairing bonfire.

The explanation provided by FCC may sound a bit confusing, but it basically means that:

• We get a string as input, this string can contain the characters A, T, C or G.
• There are two possible pairs, AT or CG.
• For every character in the input string, return it’s pair, so, if we get an A, the pair would be T, if we get a G, the pair would be C and so on.
• Return the result as an array of arrays, here’s a few examples:
```pair('CGCAT');  //-> [[C, G], [G, C], [C, G], [A, T], [T, A]]

pair('GCT');    //-> [[G, C], [C, G], [T, A]]

pair('AATG');   //-> [[A, T], [A, T], [T, A], [G, C]]```

Seems simple enough, let’s get to it! First of all, we should split our string and turn it into a array, so we can iterate over each element and get it’s pair, and then loop through every element:

```function pair(str) {
str = str.split('');
for (var i = 0; i < str.length; i++) {
// Do something with str[i];
}
}```

Now, let’s create a new function with a switch statement, where we get the pair for every element and return it.

```function pair(str) {
str = str.split('');
for (var i = 0; i < str.length; i++) {
// Do something with str[i];
}
function getPair(element) {
switch(element) {
case 'A':
return 'T';
case 'T':
return 'A';
case 'C':
return 'G';
case 'G':
return 'C';
default:
return 0;
}
}
}```

Now, we are going to create a new variable where the result is going to get stored and start constructing the result array inside the for loop by pushing an array containing the current element and their pair (by using our newly created getPair function):

```function pair(str) {
str = str.split('');
var result = [];
for (var i = 0; i < str.length; i++) {
result.push([str[i], getPair(str[i])]);
}
function getPair(element) {
switch(element) {
case 'A':
return 'T';
case 'T':
return 'A';
case 'C':
return 'G';
case 'G':
return 'C';
default:
return 0;
}
}
return result;
}```

Notice that we add the return line at the end! Try coming up with new, original methods (try using functional programming for example) of getting this problem done, it’s the best thing you can do to improve your skills!

Tagged with: