# DNA Pairing (AT, CG)

**URL:** <https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 20, 2023, 1:25am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189 "2023-05-20T01:25:52Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zuko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zuko/32/335385_2.png) [@Zuko](https://forum.freecodecamp.org/u/Zuko)\
**Post date:** [May 20, 2023, 1:25am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/1 "2023-05-20T01:25:52Z")

</div>

```auto
function pairElement(str) {

// [A,] push "T", [T,] push "A", [C,] push "G", [G,] push "C"
// Each letter in original str needs to be turned into it's own array
 
  let string = str.split(""); 

  for (let i = 0; i < string.length; i += 1); {
    string.push(); 
  } 
  return string;
}

console.log(pairElement("GCG"));

```

I think I have a good idea of what needs to happen, but I don’t know where to go from here. Can someone help me out without giving me the answer?

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [May 20, 2023, 5:26am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/2 "2023-05-20T05:26:01Z")

</div>

You want to return a new array that consists of sub arrays containing two letters. The first letter you already know. You need to add the second letter (the first letter’s pair). So you’ll need to create a new array and push the sub arrays to that new array.

---

<div class="post-metadata">

**Author:** ![Zuko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zuko/32/335385_2.png) [@Zuko](https://forum.freecodecamp.org/u/Zuko)\
**Post date:** [May 21, 2023, 3:57am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/3 "2023-05-21T03:57:22Z")

</div>

I have played around with this problems for over a day now and I am still getting nowhere. It’s crazy how with unlimited resources (excluding taking the easy way out and looking at answers)…I still can’t come up with a solution. I am struggling to get my code to understand that each letter in the original array needs to be turned into its own subarray and push the letter linked to it at the end

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [May 21, 2023, 4:13am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/4 "2023-05-21T04:13:52Z")

</div>

Can you show us your most recent code?

---

<div class="post-metadata">

**Author:** ![Zuko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zuko/32/335385_2.png) [@Zuko](https://forum.freecodecamp.org/u/Zuko)\
**Post date:** [May 21, 2023, 4:27am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/5 "2023-05-21T04:27:50Z")

</div>

Yea no problem. It has changed a lot throughout today.

```auto
function pairElement(str) {
  
  let string = str.split(""); 
  let newArr = [{"A":"T","T":"A","C":"G","G":"A"}]
  var pairs = []; 

  for (let i = 0; i < string.length; i += 1); {
    if (newArr == string) {
      pairs.push(sub.array(newArr))
    }
  } 
  return pairs;
}

console.log(pairElement("GCG"));

```

Feels like I have no clue what I am doing

---

<div class="post-metadata">

**Author:** ![krze.tomas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krze.tomas/32/265321_2.png) [@krze.tomas](https://forum.freecodecamp.org/u/krze.tomas)\
**Post date:** [May 21, 2023, 5:05am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/6 "2023-05-21T05:05:21Z")

</div>

Hey there again,  
I would suggest not to spend so much time on one problem as you can get into rabbit holes you didnt want to end up before. Try to google, navigate to the help section with tips.  
[Help section](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-dna-pairing/16009)  
console.log your function and see for yourself what is the output:

```auto
console.log(pairElement("GCG"))

```

you will get an empty array and thats because of the condition again. I would suggest to stop going forward and go back to basics about objects, arrays, loops, es6, if else conditions etc.

Or, if you still insist on pushing forward, make this:  
for each challenge you have failed, write down the correct ways how to do it from the hints section and repeat these every day like your life depends on it. Each day - start on your own, if you can remember, if not after 3 mins open your notes, memorize and rewrite and go on the next one. Eventually the ahaa moment will come

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [May 21, 2023, 5:10am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/7 "2023-05-21T05:10:38Z")

</div>

Telling people to try for 3 minutes then try to memorize answers is terrible advice. Programming is about working through problems to develop solutions. It’s ok to take your time and it’s ok to talk to other people to get their guidance on problems. Memorization won’t really help build useful programming skills.

---

<div class="post-metadata">

**Author:** ![krze.tomas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krze.tomas/32/265321_2.png) [@krze.tomas](https://forum.freecodecamp.org/u/krze.tomas)\
**Post date:** [May 21, 2023, 5:18am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/8 "2023-05-21T05:18:00Z")

</div>

Hello Jeremy,  
Thank you for your input,  
I did write that because I assumed that he is too far for what he understands and he wanted to just move forward. My first choice was that he gets back and studies more about the basics.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [May 21, 2023, 5:20am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/9 "2023-05-21T05:20:16Z")

</div>

> [@Zuko](#):
>
> ```auto
> // [A,] push "T", [T,] push "A", [C,] push "G", [G,] push "C"
> // Each letter in original str needs to be turned into it's own array
>  
> 
> ```

This is the right idea but it seems you’ve drifted from that.

Two smaller questions

1. can you make a loop that prints out to the console every letter of `str`

2. can you add inside of that loop some logic to print out the pair letter (if `str` has A print T, etc)

Those two pieces would bring you much closer

---

<div class="post-metadata">

**Author:** ![Zuko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zuko/32/335385_2.png) [@Zuko](https://forum.freecodecamp.org/u/Zuko)\
**Post date:** [May 21, 2023, 5:28am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/10 "2023-05-21T05:28:34Z")

</div>

For the first question, I just want to make sure I understand correctly. You’re saying can I make a for loop that takes everything in the given `str` and returns it `split` into individual letters that aren’t connected? So if a string is `"GCG"` the loop would return `['G', 'C', 'G']`. Or are you asking me to make a loop that returns each letter as it’s own array…like `["G"],["C"],["G"]`?

Ahhhh yes, so basically within the loop, I would add if statements for each letter?

---

<div class="post-metadata">

**Author:** ![Zuko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zuko/32/335385_2.png) [@Zuko](https://forum.freecodecamp.org/u/Zuko)\
**Post date:** [May 21, 2023, 6:04am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/11 "2023-05-21T06:04:17Z")

</div>

```auto
function pairElement(str) {
  
let string = str.split(""); 
  
if (string.contains("A")) {
    System.out.println("T");
} else if (string.contains("T")) {
    System.out.println("A");
} else if (string.contains("C")) {
    System.out.println("G");
} else if (string.contains("G")) {
    System.out.println("C");
} else {
    System.out.println(string);
} 

console.log(pairElement("GCG"));

```

Another bad attempt 🫠

---

<div class="post-metadata">

**Author:** ![Zuko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zuko/32/335385_2.png) [@Zuko](https://forum.freecodecamp.org/u/Zuko)\
**Post date:** [May 21, 2023, 8:24am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/12 "2023-05-21T08:24:20Z")

</div>

```auto
function pairElement(str) {
  
let dnaPairs = []; 

for (let i = 0; i < str.length; i += 1) {
    if (str[i] === "A") dnaPairs.push([str[i],"T"]); 
    if (str[i] === "T") dnaPairs.push([str[i],"A"]);
    if (str[i] === "C") dnaPairs.push([str[i],"G"]);
    if (str[i] === "G") dnaPairs.push([str[i],"C"]);
}
return dnaPairs; 
}

console.log(pairElement("GCG"));

```

I should’ve seen this sooooooo sooner 😭 FINALLY

---

<div class="post-metadata">

**Author:** ![ptgsdace210744](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ptgsdace210744/32/335066_2.png) [@ptgsdace210744](https://forum.freecodecamp.org/u/ptgsdace210744)\
**Post date:** [June 9, 2023, 5:20am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/13 "2023-06-09T05:20:30Z")

</div>

Mod Edit: SOLUTION REMOVED

---

<div class="post-metadata">

**Author:** ![bappyasif](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bappyasif/32/190571_2.png) [@bappyasif](https://forum.freecodecamp.org/u/bappyasif)\
**Post date:** [June 9, 2023, 10:53am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/14 "2023-06-09T10:53:27Z")

</div>

?!?!  
at least use a spoiler or something before showing your solution code.

and besides, its recommended to help people to get to those answers rather giving working solution

happy learning to us all 🙂

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 9, 2023, 11:18am UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/15 "2023-06-09T11:18:40Z")

</div>

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [December 8, 2023, 11:19pm UTC](https://forum.freecodecamp.org/t/dna-pairing-at-cg/611189/16 "2023-12-08T23:19:08Z")

</div>

Closed on December 8, 2023, 11:19pm UTC

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