DNA Pairing, problem it's correct but?

Hi everybody

I have a problem with this challenge, it seems to be correct because when I test it on the developer tool console it works but on FCC console doesn’t work, where do you think the problem is?

here is the code:


function pairElement(str) {
  let toArr = str.split("");
  let newArr = [];
  for(let i = 0; i < str.length; i++){
    if(toArr[i] === "C" || toArr[i] === "G"){
      if(toArr[i] === "C"){
        newArr.push(["CG"]);
      }else{
        newArr.push(["GC"]);
      }
    }
    if(toArr[i] === "A" || toArr[i] === "T"){
      if(toArr[i] === "A"){
        newArr.push(["AT"]);
      }else{
        newArr.push(["TA"]);
      }
    }
  }
  return newArr;
}

pairElement("GCG");

Look at the browser’s console instead of the “fake” FCC console.

pairElement(“GCG”) should return the following:

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

Instead, your function returns the following:

[ [ "GC" ], ["'CG" ], ["'GC" ] ]
1 Like

here is it again but doesn’t work again


function pairElement(str) {
  let toArr = str.split("");
  let newArr = [];
  for(let i = 0; i < str.length; i++){
    if(toArr[i] === "C" || toArr[i] === "G"){
      if(toArr[i] === "C"){
        newArr.push(["C", "G"]);
      }else{
        newArr.push(["G", "C"]);
      }
    }
    if(toArr[i] === "A" || toArr[i] === "T"){
      if(toArr[i] === "A"){
        newArr.push(["A", "T"]);
      }else{
        newArr.push(["T", "A"]);
      }
    }
  }
  console.log(newArr);
}

pairElement("GCG");

@ansdb Your function needs a return statement (instead of just a console.log statement).

1 Like

Yes i don’t pay attention to that, thank you so much for your help i have one question for you

is my code can be considered as a best practice or clever code or there is other ways to write code more organized, short and fast than mine?

If you click the Get A Hint button on the challenge, the Intermediate Solution provides a more concise solution. To be considered concise, you do not want to see “similar” code written through out the function. You have several newArr.push statements and several if statements which are similar in nature. Imagine if there were 100 more possible letter mappings how much more code you would have to write using your approach vs. the Intermediate Solution’s code. The only thing you would have to change in the Intermediate solution would be the pairs object.

1 Like

Yes you’re right, thank you so much mr @RandellDawson for your help :slight_smile: