Skip to content

Commit 146a107

Browse files
committed
Complete DNA pairing algorithm
1 parent 0e26c10 commit 146a107

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Repository for examples of typical algorithm solutions and data structures in Ja
88

99
## Freecodecamp: Javascript Algorithms & Data Structures
1010

11-
Algorithms from the Freecodecamp Javascript coursework
11+
Algorithms from the Freecodecamp Javascript algorithms and data structures certification coursework
1212

1313
- [Basic Algorithms](https://github.com/ahcode0919/javascript-algorithm-ds/blob/master/src/fcc-basic-algorithms/fcc-basic-algorithms.md#freecodecamp-basic-algorithms)
1414
- [Intermediate Algorithms](https://github.com/ahcode0919/javascript-algorithm-ds/blob/master/src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md#freecodecamp-intermediate-algorithms)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function pairElements(str) {
2+
const pair = function(element) {
3+
switch (element) {
4+
case 'A':
5+
return 'T';
6+
case 'T':
7+
return 'A';
8+
case 'C':
9+
return 'G';
10+
case 'G':
11+
return 'C';
12+
default:
13+
return '';
14+
}
15+
}
16+
17+
const elements = str.split('');
18+
let pairs = [];
19+
for (let element of elements) {
20+
pairs.push([element, pair(element)]);
21+
}
22+
return pairs;
23+
}

src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function destroyer(arr, ...values) {
5858

5959
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.
6060

61-
For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], and the second argument is { last: "Capulet" }, then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument.
61+
For example, if the first argument is `[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]`, and the second argument is `{ last: "Capulet" }`, then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument.
6262

6363
```javascript
6464
export function whatIsInAName(collection, source) {
@@ -135,3 +135,41 @@ export function myReplace(str, before, after) {
135135
return [preInsert, after, postInsert].join('');
136136
}
137137
```
138+
139+
#### DNA Pairing
140+
141+
The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.
142+
143+
Base pairs are a pair of `AT` and `CG`. Match the missing element to the provided character.
144+
145+
Return the provided character as the first element in each array.
146+
147+
For example, for the input `GCG`, return `[["G", "C"], ["C","G"],["G", "C"]]`
148+
149+
The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.
150+
151+
```javascript
152+
export function pairElements(str) {
153+
const pair = function(element) {
154+
switch (element) {
155+
case 'A':
156+
return 'T';
157+
case 'T':
158+
return 'A';
159+
case 'C':
160+
return 'G';
161+
case 'G':
162+
return 'C';
163+
default:
164+
return '';
165+
}
166+
}
167+
168+
const elements = str.split('');
169+
let pairs = [];
170+
for (let element of elements) {
171+
pairs.push([element, pair(element)]);
172+
}
173+
return pairs;
174+
}
175+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { pairElements } from "../../src/fcc-intermediate-algorithms/dna_pairing";
2+
3+
test('should pair elements', () => {
4+
expect(pairElements("ATCGA")).toEqual([["A","T"],["T","A"],["C","G"],["G","C"],["A","T"]]);
5+
});

0 commit comments

Comments
 (0)