DNA Pairing (AT, CG)

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); {
  return string;


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?

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.

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

Can you show us your most recent code?

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

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) {
  return pairs;


Feels like I have no clue what I am doing

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
console.log your function and see for yourself what is the output:


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

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.

1 Like

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.

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

1 Like

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?

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


Another bad attempt :melting_face:

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; 


I should’ve seen this sooooooo sooner :sob: FINALLY


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 :slight_smile:

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.

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