DNA Pairing Challenge (2d Array)

Hello everyone!

I am currently working on this challenge for a few hours already, where I have an input of str = “GCG”. and should return this:

[["G", "C"], ["C","G"], ["G", "C"]]

This is the code I have so far:

function pairElement(str) {
  let row = 3;
  let column = 2;
  let array = [];
  let h = 0;

  for (let i = 0; i < row; i++) {
    array[i] = [];
    for (let j = 0; j < column; j++) {
      array[i][j] = str[h++];
  //return array;


And with this code, I am getting this output:

[ [ 'G', 'C' ], [ 'G', undefined ], [ undefined, undefined ] ]

I have been checking Google and tried some changes but nothing. And before having to check the solution I would love some guidance from you guys.

I feel that is something “easy” that I am forgetting. Thank you in advance!

Could you explain how the code should work, in a more step-by-step manner? Where from do the row and column values come from?

1 Like

i don’t see any clear pairing/matching method … to convert ACTG to its pair…
A >> T
T >> A
C >> G
G >> A

1 Like

you can do this instead :slight_smile:

MOD EDIT: Solution removed.

Thank you for replying. I was googling about this subject and found those rows and columns variables and they made sense to me to use to make the arrays. Honestly, I don’t have an explanation for those variables, just took them from a website. Probably that’s not a good way to do things to become a developer, sorry about my lack of explanation.

Hello! That’s what I thought, just I don’t know how to implement that in the code :confused:

Try splitting the task into smaller pieces. Some help with that can give imagining how you’d solve such task by hand - on piece of paper.

1 Like

One option is to create an object (or a Map) where each letter is the key and the value is the corresponding array.

const grades = {
  "A": ["A+", "A", "A-"],
  "B": ["B+", "B", "B-"],
  "C": ["C+", "C", "C-"],

console.log(grades["A"]); // ["A+", "A", "A-"]

There are other ways of “mapping values” but this is a pretty common approach.

Thank you for the tip!! I will be more focus on having a good pace and understanding how to handle the challenges. I think I am just too focused on trying to pass them fast instead understand the code.

I test my understanding of the challenge by re-do immediately after passed.
Focus on the method to solve challenge. Those experience in breaking down problems and find relevant function/tools will be precious!

That’s a good way also to do it. Because sometimes right away I forgot what I did lol

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