Solving "DNA Pairing" / freeCodeCamp Algorithm Challenges
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-07-20 16:50:57

---
title: Solving "DNA Pairing" / freeCodeCamp Algorithm Challenges
published: true
description: My guide, notes, and solution to freeCodeCamp's intermediate algorithm challenge, "DNA Pairing" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-024-dna-pairing
series: Solving freeCodeCamp's Algorithm Challenges
---

![DNA Pairing](//images.ctfassets.net/yzwpq1epaq68/8PVmB0kjrvPNNghuPT6uL/d04e843d85d2ef1b07d2b17ef3515727/ss-024.png)

Let's solve freeCodeCamp's intermediate algorithm scripting challenge, '*DNA Pairing*'.



### Starter Code

```javascript
function pairElement(str) {
  return str;
}

pairElement("GCG");
```



### Instructions

The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.

[Base pairs](http://en.wikipedia.org/wiki/Base_pair) 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.



### Test Cases

```
pairElement("ATCGA") should return [["A","T"],["T","A"],["C","G"],["G","C"],["A","T"]].
pairElement("TTGAG") should return [["T","A"],["T","A"],["G","C"],["A","T"],["G","C"]].
pairElement("CTCTA") should return [["C","G"],["T","A"],["C","G"],["T","A"],["A","T"]].
```



# Our Approach

After reading the starter code, instructions, and test cases, this is what I summarized about this challenge - 

- We have one input, a string.
- We must return an array (a 2D array).
- We have base pairs which we must evaluate with the input string. We have to create an array and add appropriate pairs/arrays.



Hey! At least no more RegEx!



So after reading a little more about base pairs, if we come across a certain letter, we will have to find its partner, and insert that pair into our new array. 



My first action is to create an empty array to house all the pairs we will have to make.

`let pairs = [];`



Before we dive into to the challenge further, let's just quickly figure how the pairing works -

- 'A' = ['A', 'T']
- 'T' = ['T', 'A']
- 'C' = ['C', 'G']
- 'G' = ['G', 'C']



So we have four cases (or for if/elses to evaluate). As the example in the instructions mentions, a test case would be 

`str = 'GCG', return [["G", "C"], ["C","G"],["G", "C"]]`



I figure the best way to evaluate `str`, since its a string, is to `split('')` into an array. Then we can use a method to loop over each case. I think using `map()` is a good way to go about it.

[Array.map() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map "Array.map() docs")

```javascript
str = 'GCG';
str.split('');
// Array(3) [ "G", "C", "G" ]
```



So now, using `map()`, we can evaluate each item ('G', 'C', etc.) and see if its the correct case, we can pair it with the correct pairing in an array and push the pair into our `pairs` array (which is empty for now).



[switch statement (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch "MDN switch")

```javascript
["G","C","G"].map(elem => {
  switch (elem) {
    case 'C':
      pairs.push(['C', 'G'])
      break;
    case 'G':
      pairs.push(['G', 'C'])
      break;
  	....
	}
})
```



Another way which would work is an if/else statement.

Remember to `return pairs` (`pairs` being the new array)!



# Our Solution

```javascript
// Two Solutions - 1) if/else, 2) switch

function pairElement(str) {
  let pairs = [];
  str.split('').map(elem => {
    if (elem === 'G') {
      pairs.push(['G', 'C'])
    }
    else if (elem === 'C') {
      pairs.push(['C', 'G'])
    }
    else if (elem === 'A') {
      pairs.push(['A', 'T'])
    }
    else if (elem === 'T') {
      pairs.push(['T', 'A'])
    }
  })
  return pairs;
}

// Second solution
function pairElement(str) {
  let pairs = [];
  const strArr = str.split("");
  strArr.map(elem => {
    switch (elem) {
    case 'C':
      pairs.push(['C', 'G']);
      break;
    case 'G':
      pairs.push(['G', 'C']);
      break;
    case 'A':
      pairs.push(['A', 'T']);
      break;      
    case 'T':
      pairs.push(['T', 'A']);
      break;
    }
  })
  return pairs;
}  

pairElement("GCG");
```



# Links & Resources

['DNA Pairing' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing "Intermediate Algorithm Scripting: DNA Pairing on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/024---dna-pairing.js "GitHub Solution")



Thank you for reading!