DNA Pairing output validating issue

Tell us what’s happening:
The test cases aren’t approved inspite of getting identical output to what is required. Attaching an image for reference.

Your code so far

var completedSequence = [];
function pairElement(str) {
  var givenSequence = str.split('');
  for ( var i = 0; i < givenSequence.length; i++ ){
    var tempArray = [];
    tempArray.push(givenSequence[i]);
    if ( basePairs[0].indexOf(givenSequence[i]) >= 0 ) {
      if ( basePairs[0].indexOf(givenSequence[i]) == 0 ) {
        tempArray.push(basePairs[0][1]);
        completedSequence.push(tempArray);
      } else {
        tempArray.push(basePairs[0][0]);
        completedSequence.push(tempArray);
      }
    } else {
      if ( basePairs[1].indexOf(givenSequence[i]) == 0 ) {
        tempArray.push(basePairs[1][1]);
        completedSequence.push(tempArray);
      } else {
        tempArray.push(basePairs[1][0]);
        completedSequence.push(tempArray);
      }
    }
  }
  return completedSequence;
}

pairElement("ATCGA");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0.

Link to the challenge:
https://www.freecodecamp.org/challenges/dna-pairing

The variable completedSequence is in the global scope which interferes with the automated tests. Move that inside your function and it should work.

Thanks, it worked.
But why does it cause a problem? Just being curious.

The code that you write is loaded into the tests once and then they call your function as many times as needed. This means completedSequence gets initialized to an empty array only once, and it will keep all of the values pushed into it. When you moved it into your function, it gets initialized every time the function is called so you’re starting with a fresh array.

1 Like