Feedback on logic before approaching DNA Pairing

Tell us what’s happening:

Hello, I don’t have any code written yet. Rather, I’d like to review my approach before I begin. Below I have my ideas for how I should proceed. I’d appreciate people critiquing my logic and providing helpful corrections for ways of thinking about particular aspects of the problem I’m addressing (or maybe aspects I’m dismissing and should reconsider).

Thank you for your input.


Your code so far

function pairElement(str) {
  return str;


1.create empty array to push different subarrays into
2.create number of subarrays equal to elements in str
3.subarray size should be no bigger than 2 elements.  
4.If they are bigger than 2 elements then they are spliced off and pushed into a new array.  
5.These subarrays once filled are pushed into larger array
6.That array is returned

This challenge seems similar to Chunky Monkey, so I reviewed that challenge to see if I could get some inspiration.


Your browser information:

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

Link to the challenge:

This is too complicated, there is no need to check lengths or splice anything. This doesn’t involve chunking: what you are doing is taking a string, and changing it to an array of pairs (each character + their pair). So for example:

SET output to empty array
FOR each character in the string
  CASE character OF
    A: push [A, T] to output
    T: push [T, A] to output
    ... do same for other two characters
RETURN output

This is easier still with map, just have map take a function that does what the case statement does in the pseudocode

I don’t quite understand why it’s returning str is not a function. It seems as if I’m doing everything appropriately:

  1. I’m using arrow function so I’ve checked the arrangement of brackets and parentheses and that seems correct
  2. I’ve named named val
  3. I’m using val as a parameter and passing that through the if statement and then using it along with it’s complementary to push into the empty array

I suppose there’s something up with the way I’m using str, but I’m not sure what that would be. Also, perhaps it’s that I rename val so many times. I just thought of that. Is that it?
I’m also concerned about chaining so manner if statements together. It seems to complicated and there is definitely a simpler way that I’m not saying. I think that I’m not utilizing map() in the most productive way. I’ve been several different docs, but I’m not seeing where I’m off. Help please. (Code below.)

function pairElement(str) {
  var newArray = [];

newArray = => {
if(val = "C") {newArray.push(val, "G")}
if(val = "G") {newArray.push(val, "C")}
if(val = "T") {newArray.push(val, "A")}
if(val = "A") {newArray.push(val, "T")}
  return newArray;


str is a string.

map() is method for an array

Add a split() and you are there

What @ilenia said. Also you’re not actually using map here: the point of map is that it takes an array of one kind of thing and returns a new array with all the elements ‘mapped’ to another kind of thing.

newArray = []
loop over string and push to newArray
return newArray

The above is where you would use a loop (for, for…of, while or forEach). A map operation is not just a replacement for a loop: you can do what you’re doing, but there’s no point using map if that is that case.

function mapperFunction(character) {
  if character is foo:
    return [foo, fooMatchedCharacter]
  if character is bar:
    return ...

Join array of strings and return it

Right, @DanCouper is totally right, if you want to use a method in place of a loop to iterate over an array in the way you are doing, forEach() seems what you want

1 Like

I had tried forEach before and it hadn’t passed any test, but I tried it again because it seems from what you’ve said that I was onto something.

Yet, it’s not passing. I’ve been reading the doc and I’m not sure why.

forEach performs a function for each element in the array, which is exactly what I want. I want the method to iterate through the array and push the element encountered along with its complement onto the empty array.

That’s why I thought I was doing. Something is wrong though and it returns str.forEach is not an array. I tried splitting str to ensure it was an array type and not a string type, but I’m still returning this same thing.

So, I’m perplexed. I can’t see any holes in this, although I’m still skeptical off all the if statements (I actually have a couple of questions for myself at the end that I’ve been unable to answer so far). Where is my error? Is it syntax? Logic? I can’t see it.

function pairElement(str) {
  var newArray = [];
  str.split(" ");
if(val = "C"){
  newArray.push(val, "G");
} if(val = "G"){
  newArray.push(val, "C");
} if(val = "T"){
  newArray.push(val, "A");
} if(val = "A"){
  newArray.push(val = "T")

  return newArray;

//Should I be setting up the function before I run the method??
//How would I set it up as a string of if statements?  Is there a simpler way?

With str.split(' ') you are splitting on a space, as your starting string doesn’t have a space it is not splitted.
split() doesn’t mutate the original string, so doing as follow doesn’t work either (this is the reason for which you get that error)


Try with str.split('').forEach(...)

Figured it out with for loop and forEach. Thanks for your help.