What's wrong with my for loop?

What’s wrong with my code, anybody can help me with this? Do I need a break after every push method?

Question:

DNA Pairing

The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.

Base pairs are a pair of AT and CG. Match the missing element to the provided character.

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

For example, for the input GCG , return [["G", "C"], ["C","G"], ["G", "C"]]

The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.
Your code so far


function pairElement(str) {
const newStr = [];
for (let i = 0; i < str.length; i++) {
  if (str[i] === "A") {
    newStr.push(["A", "T"])
  } else if (str[i] === "T") {
    newStr.push(["T", "A"])
  } else if (str[i] === "G") {
    newStr.push(["G", "C"])
  }
  return newStr.push(["C", "G"])
}
return newStr;
}

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

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36

Challenge: DNA Pairing

Link to the challenge:

The return keyword will exit any function it is currently in. You have this as part of your for loop so it will hit that on the first iteration and never make it to a second. There is also a slight problem with the logic there.

1 Like

Wow,get it, thanks! :laughing: :laughing: :laughing:

1 Like

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