JavaScript DNAStrand(A-T,C-G)

Description:

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

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

Examples
DNAStrand ("ATTGC") // return "TAACG"
DNAStrand ("GTAT") // return "CATA"
方法一:使用正则配对
function DNAStrand(dna){
  return dna.replace(/A/g, 't').replace(/T/g, 'a').replace(/C/g, 'g').replace(/G/g, 'c').toUpperCase();
}

console.log(DNAStrand("ATTGC"));//TAACG
console.log(DNAStrand("GTAT"));//CATA
方法二:使用 < key,value >键值对
var pairs = {'A':'T','T':'A','C':'G','G':'C'};
function DNAStrand(dna) {
  return dna.replace(/./g, function(y) {
    return pairs[y];
  });
}

console.log(DNAStrand("ATTGC"));//TAACG
console.log(DNAStrand("GTAT"));//CATA
方法三
  • split() 方法用于把一个字符串分割成字符串数组( 如:"2:3:4:5".split(":") //将返回["2", "3", "4", "5"])
  • map() 方法返回一个新数组,数组中的元素为原始数组元素按顺序依次调用函数处理后的值
array.map(function(currentValue,index,arr), thisValue)

currentValue(必须)当前元素的值
index (可选)     当前元素的索引值
arr (可选)       当前元素属于的数组对象
thisValue(可选)  对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
                  如果省略了 thisValue ,"this" 的值为 "undefined"
var pairs = {'A':'T','T':'A','C':'G','G':'C'};
function DNAStrand(dna){
  return dna.split('').map(function(y){ return pairs[y] }).join('');
}

console.log(DNAStrand("ATTGC"));//TAACG
console.log(DNAStrand("GTAT"));//CATA

转载于:https://www.cnblogs.com/kid2333/p/7443982.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值