-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdna_pairing.js
39 lines (29 loc) · 1.21 KB
/
dna_pairing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"use strict";
/*
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) {
let answer = [];
str
.split("")
.forEach((element) => {
if (element === "G") {
answer.push(["G", "C"]);
} else if (element === "C") {
answer.push(["C", "G"]);
} else if (element === "T") {
answer.push(["T", "A"])
} else {
answer.push(["A", "T"])
}
})
return answer;
}
pairElement("GCG");
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"]]