1

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) {
    mRNA.concat(b);
  }
}

function m_RNA(){
  //console.log(DNA)
  for (var i = 0; i < DNA.length; i++) {
    console.log("Checking: " + DNA[i]);
    check("T", "A", DNA[i]);
    check("A", "U", DNA[i]);
    check("C", "G", DNA[i]);
    check("G", "C", DNA[i]);
    console.log(mRNA);
  }
}

Its supposed to show AUC in the console but its just blank. This is through firefox by the way.

5
  • 2
    The + operator concatenates strings: "hello" + " " + "world" is "hello world" (which you apparently already know because you use that in a console.log() call) Commented Apr 8, 2019 at 19:29
  • 4
    Strings are immutable. So you need to save the concatenated version back to the variable: mRNA = mRNA.concat(b) Commented Apr 8, 2019 at 19:30
  • 1
    Also the if test in check() needs to use == not = Commented Apr 8, 2019 at 19:31
  • @Pointy The variable is still showing up as blank. Commented Apr 8, 2019 at 19:31
  • @MarkMeyer Thank you! It's solved! Commented Apr 8, 2019 at 19:32

2 Answers 2

6

mRNA.concat(b); doesn't mutate the string, it only computes the value. You need tomRNA = mRNA.concat(b) (or mRNA = mRNA + b) to change the value of mRNA.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.