For loop and if statement pair not working

Tell us what’s happening:
Please help with the DNA pairing challenge for algos. It’s supposed to return a 2d array of GC and TA pairs in the correct sequence from a given half pair string.

I have console logged all the steps (i think) of this code to find out why it is not working but it seems that the individual bits are supposed to work. What am I missing?

PS. I could look at the given solutions but before that I need to understand what I am missing with mine.

Thank you!

  **Your code so far**

function pairElement(str) {
let arr = [];
//define generic pairs
let arrGc = ['G', 'C'];
let arrTa = ['A', 'T'];

//push generic pairs in order
for (let i = 0; i < str.length; i++) {
  str[i].match(/G|C/g) ? arr.push(arrGc) : arr.push(arrTa);
//correct the pair sequence
  if (arr[i][0] !== str[i]) {
    arr[i].reverse();
      }
    }

return arr;
}

pairElement("ATCGA");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0

Challenge: DNA Pairing

Link to the challenge:

are you doing this part?

Return the provided character as the first element in each array.

1 Like

You’ve got a tricky little bug here. Let’s look at a simple example:

pairElement("GC")

Your code does this twice:

arr.push(arrGc)

In the first step of the loop, you push ['G', 'C'] to the result array. The if statement is false so the loop proceeds to the next step.

Now you push ['G', 'C'] again, but the array that you push is THE SAME as the first one, because arrays are passed by reference. The two arrays are literally the same in memory.

This time, your if statement is true, and you reverse the array. But because both arrays are the same entity, you reverse both arrays.

You can easily fix it:

arr.push([...arrGc]) : arr.push([...arrTa])

What’s happening now is that each time you push to the result array, you push a completely new array. When you manipulate/reverse it, you can be sure that you’re only reversing this particular array, not accidentally many of them.


If you’re confused, that’s normal. Maybe this example makes it clearer:

let arrayOne = ['a', 'b'];
let arrayTwo = arrayOne;


// manipulate the second array:
arrayTwo[0] = 'pesky bug';


// because both arrays are the same entity, you also accidentally manipulated arrayOne:
console.log(arrayOne) // ['pesky bug', 'b']

The stuff to google is “pass by reference vs pass by value”.

1 Like

Gee… it didn’t occur to me that .push() doesn’t create a new array (in a new address). Your explanation was spot on thanks so much!

since the DNA pair is only two characters, I figured I’d just use an if statement to do that.

So yes but @jsdisco showed me how .push() 's pass by reference had past over my head.

You can also just push the literal arrays instead.

arr.push(['G', 'C']) : arr.push(['A', 'T'])
1 Like

this just solidified my understanding. I had somehow thought the issue was with the push() method passing it’s arguments as references in general (now that I think of that it makes no sense). I now see the issue is at the arr[i][0].reverse() bit.

Thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.