DNA Pairing Challenge validation

Hi!

When I run my code the return value matches the expected but don’t validates! Here is the code

var ins;
var pair;
var res = [];
function pairElement(str) {
  var inp = str.split("");
  for (i = 0;i < inp.length;i++) {
    switch(inp[i]) {
      case 'A':
        pair = 'T';
        break;
      case 'T':
        pair = 'A';
        break;       
      case 'C':
        pair = 'G';
        break;    
      case 'G':
        pair = 'C';
        break;           
    }
    ins = [inp[i],pair];
    res.push(ins);
  }  
  return res;
}

pairElement("CTCTA");

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

1 Like

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the new value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
2 Likes

Thanks you so much! Crystal clear now