-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex.2.html
87 lines (77 loc) · 2.99 KB
/
Ex.2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Fira+Sans+Extra+Condensed" rel="stylesheet">
<title>Document</title>
</head>
<body>
<img src="https://www.codewars.com/users/stefan835/badges/large" alt="codewars-badge">
<h1>Ex.2</h1>
<h3><a href="./Ex.1.html">Link to previos!</a></h3>
<h3><a href="./Ex.3.html">Link to next!</a></h3>
<p>Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the
development and functioning of living organisms.</p>
<p>If you want to know more http://en.wikipedia.org/wiki/DNA</p>
<p>In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". You have function with one side of
the DNA (string, except for Haskell); you need to get the other complementary side. DNA strand is never empty or
there is no DNA at all (again, except for Haskell).</p>
<i>DNAStrand ("ATTGC") # return "TAACG"</i>
<i>DNAStrand ("GTAT") # return "CATA"</i>
<h2>My solution</h2>
<div class="code">
<pre>function DNAStrand(dna) {<br/> var nucleobases = dna.split('');<br/> nucleobases = nucleobases.map(function (nucleobase) {<br/> switch (nucleobase) {<br/> case "A":<br/> return "T";<br/> case "T":<br/> return "A";<br/> case "C":<br/> return "G";<br/> case "G":<br/> return "C";<br/> default:<br/> return<br/> }<br/> });<br/> var result = nucleobases.toString().replace(/,/g, "");<br/> return result<br/>}</pre>
</div>
<h2>Best solution</h2>
<div class="code">
<pre>function DNAStrand(dna) {<br/>return dna.replace(/./g, function(c) {<br/>return DNAStrand.pairs[c]<br/>})<br/>}<br/><br/>DNAStrand.pairs = {<br/>A: 'T',<br/>T: 'A',<br/>C: 'G',<br/>G: 'C',<br/>}</pre>
</div>
<h2 class="passed">PASSED</h2>
<h3>Time: 350ms</h3>
<script>
//MY SOLUTION
function DNAStrand(dna) {
var nucleobases = dna.split('');
nucleobases = nucleobases.map(function (nucleobase) {
switch (nucleobase) {
case "A":
return "T";
case "T":
return "A";
case "C":
return "G";
case "G":
return "C";
default:
return
}
});
var result = nucleobases.toString().replace(/,/g, "");
return result
}
console.log(DNAStrand("ATTGC"));
console.log(DNAStrand("GTAT"));
DNAStrand("ATTGC");
/*# return "TAACG"*/
DNAStrand("GTAT")
/* # return "CATA"*/
//BEST SOLUTION
// function DNAStrand(dna) {
// return dna.replace(/./g, function(c) {
// return DNAStrand.pairs[c]
// })
// }
//
// DNAStrand.pairs = {
// A: 'T',
// T: 'A',
// C: 'G',
// G: 'C',
// }
</script>
</body>
</html>