DNA Pairing Solution with if/else Chain

When I run the code I get this message:
The code you have written is taking longer than the 2500ms our challenges allow. You may have created an infinite loop or need to write a more efficient algorithm

What am I doing wrong?

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

console.log(pairElement("GCG"));
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36`.

Challenge: DNA Pairing

Your loop runs as long as `i` is smaller than `arr.length`, but with each step youâ€™re pushing a new item to the array. I guess you see where this is headingâ€¦

2 Likes

Oh I need to create a NEW array and push to that?

I just tried pushing to a new array but that still didnâ€™t work.

``````  var arr = str.split();
var result = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] === "A") {
result.push(["A", "T"]);
} else if (arr[i] === "T") {
result.push(["T", "A"]);
} else if (arr[i] === "C") {
result.push(["C", "G"]);
} else {
result.push(["G", "C"]);
}
}
return result;
}
``````

It just gives me the first subarray:

``````[ [ 'G', 'C' ] ]
``````

Iâ€™ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the â€śpreformatted textâ€ť tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (â€™).

1 Like

you may want to check whatâ€™s `arr` value, and how `split` works

1 Like

Okay I will make sure to do that. Sorry Iâ€™m still very new to asking questions on FCC.

1 Like

But doesnâ€™t str.split() just split the string by each letter into an array?

it could do that, but is it doing that now?
check your values, check the documentation

1 Like

OH RIGHT. You need to do str.split("")!
You have to enter an empty string.
I totally forgot. And it works now!
Thanks, such a silly mistake.

awesome! good job

1 Like