알고리즘 41 - DNA to RNA Conversion

jabae·2021년 10월 25일
0

알고리즘

목록 보기
41/97

Q.

Deoxyribonucleic acid, DNA is the primary information storage molecule in biological systems. It is composed of four nucleic acid bases Guanine ('G'), Cytosine ('C'), Adenine ('A'), and Thymine ('T').

Ribonucleic acid, RNA, is the primary messenger molecule in cells. RNA differs slightly from DNA its chemical structure and contains no Thymine. In RNA Thymine is replaced by another nucleic acid Uracil ('U').

Create a function which translates a given DNA string into RNA.

For example:

"GCAT" => "GCAU"
The input string can be of arbitrary length - in particular, it may be empty. All input is guaranteed to be valid, i.e. each input string will only ever consist of 'G', 'C', 'A' and/or 'T'.

A)

function DNAtoRNA(dna) {
  let result = "";
  
  for (let el in dna) {
    if (dna[el] === 'T')
      result += 'U';
    else
      result += dna[el];
  }
  return result;
}

other

.replace()로 교체해주거나

function DNAtoRNA(dna){
  return dna.replace(/T/g, 'U');
}

.split()으로 교체될 알파벳으로 나눈 후 .join()을 바꿀 알파벳으로 붙여주는 솔루션도 있다!

function DNAtoRNA(dna) {
  return dna.split("T").join("U");
}
profile
it's me!:)

0개의 댓글