-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDNAPairing.js
52 lines (41 loc) · 1.33 KB
/
DNAPairing.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
40
41
42
43
44
45
46
47
48
49
50
51
52
// Pairs of DNA strands consist of nucleobase pairs. Base pairs are represented by the
// characters AT and CG, which form building blocks of the DNA double helix.
// The DNA strand is missing the pairing element. Write a function to match the missing
// base pairs for the provided DNA strand. For each character in the provided string, find
// the base pair character. Return the results as a 2d 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.
// Method 1
// function pairElement(str) {
// const matchWithBasePair = function(char) {
// switch (char) {
// case "A":
// return ["A", "T"];
// case "T":
// return ["T", "A"];
// case "C":
// return ["C", "G"];
// case "G":
// return ["G", "C"];
// }
// };
// const pairs = [];
// for (let i = 0; i < str.length; i++) {
// pairs.push(matchWithBasePair(str[i]));
// }
// return pairs;
// }
// pairElement("GCG");
function pairElement(str) {
const pairs = {
A: "T",
T: "A",
C: "G",
G: "C"
};
return str
.split("")
.map(x => [x, pairs[x]]);
}
console.log(pairElement("GCG"));