How to append or concat strings in Javascript?

后端 未结 2 1211
感动是毒
感动是毒 2021-01-26 18:33

So I\'m trying to add to a string and it shows up as empty.

var DNA = \"TAG\";
var mRNA = \"\";

m_RNA()

function check(a, b, string) {
  if (string = a) {
             


        
2条回答
  •  梦谈多话
    2021-01-26 18:59

    Rather than trying to mutate variables on the upper scope, you could consider to wrap your task into a function...

    let's refactor it making sure m_RNA returns the correct mapping:

    var DNA = "TAG";
    
    // create a map so every char maps to something.
    const map = {
      T: 'A',
      A: 'U',
      C: 'G',
      G: 'C',
    };
    
    // check only needs to pick from map, or return an empty string.
    function check(fragment) {
      return map[fragment] || '';
    }
    
    
    function m_RNA(dna) {
      // reduces all the chars to the new sequence
      return Array.from(dna).reduce(
        function (result, fragment) {
          return result.concat(check(fragment))
        },
        "",
      );
    }
    
    var mRNA = m_RNA(DNA);
    console.log('mRNA', mRNA);

提交回复
热议问题