CodeWar(JavaScript)---Complementary DNA

9 篇文章 0 订阅

Complementary DNA

题目:

Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the “instructions” for the development and functioning of living organisms.

If you want to know more http://en.wikipedia.org/wiki/DNA

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).

翻译(来自有道翻译):

脱氧核糖核酸(DNA)是一种在细胞核中发现的化学物质,携带着生物体发育和功能的“指令”。

如果你想知道更多http://en.wikipedia.org/wiki/DNA

在DNA串中,符号“A”和“T”是互补的,如“C”和“G”。DNA的一边有功能(弦,除了Haskell);你需要得到另一个互补的边。DNA链从来不是空的,或者根本就没有DNA(再说一遍,除了Haskell)。

DNAStrand ("ATTGC") // return "TAACG"

DNAStrand ("GTAT") // return "CATA" 

个人解题代码:

//思路:定义一个空字符串str,for循环遍历dna字符串
//使用charAt()方法与"A""T""C""G"做判断,并将结果加在str中,最后输出str
function DNAStrand(dna){
  var str = "";
  for(var i = 0 ; i < dna.length ; i++){
    if(dna.charAt(i) == "A"){
      str += "T"
    }  if(dna.charAt(i) == "T"){
      str += "A"
    }  if(dna.charAt(i) == "C"){
      str += "G"
    }  if(dna.charAt(i) == "G"){
      str += "C"
    } 
  }
  
  return str
}

高亮答案:

function DNAStrand(dna) {
  return dna.replace(/./g, function(c) {
    return DNAStrand.pairs[c]
  })
}

DNAStrand.pairs = {
  A: 'T',
  T: 'A',
  C: 'G',
  G: 'C',
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值