-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm8.html
67 lines (48 loc) · 1.4 KB
/
algorithm8.html
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE html>
<html>
<body>
<script>
//https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing
//The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.
//Return the provided character as the first element in each array.
function pairElement(str) {
var arr = str.split("");
var newArr = [];
for (var i=0; i<arr.length; i++) {
if (arr[i] == "A") {
newArr.push([arr[i],"T"])
}
if (arr[i] == "T") {
newArr.push([arr[i],"A"])
}
if (arr[i] == "C") {
newArr.push([arr[i],"G"])
}
if (arr[i] == "G") {
newArr.push([arr[i],"C"])
}
}
return newArr;
}
// solution #2
//function pairElement(str) {
// //create object for pair lookup
// var pairs = {
// A: "T",
// T: "A",
// C: "G",
// G: "C"
// };
// //split string into array of characters
// var arr = str.split("");
// //map character to array of character and matching pair
// return arr.map(x => [x, pairs[x]]);
// }
//tests
console.log(pairElement("ATCGA"));
console.log(pairElement("TTGAG"));
console.log(pairElement("CTCTA"));
console.log(pairElement("GCG"));
</script>
</body>
</html>