# DNA Pair Algorithm -- Help Understaning .forEach Behavior

**URL:** <https://forum.freecodecamp.org/t/dna-pair-algorithm-help-understaning-foreach-behavior/80989>\
**Category:** General\
**Created:** [January 27, 2017, 12:58am UTC](https://forum.freecodecamp.org/t/dna-pair-algorithm-help-understaning-foreach-behavior/80989 "2017-01-27T00:58:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PinkPanther901](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@PinkPanther901](https://forum.freecodecamp.org/u/PinkPanther901)\
**Post date:** [January 27, 2017, 12:58am UTC](https://forum.freecodecamp.org/t/dna-pair-algorithm-help-understaning-foreach-behavior/80989/1 "2017-01-27T00:58:03Z")

</div>

Hi all! So I have my code working (yay!) but it appears as though it is _not_ executing top-to-bottom as I expect, and I’m not sure why. 😕

Here’s my solution: [http://bit.ly/2jWY0Iu](http://bit.ly/2jWY0Iu)

In the middle I have a “sanity check” console log that breaks the two steps in half, but when it runs it is ~actually~ logging as if the .forEach section of the code below it has already run (adding the second letter pair to the sub-array), thus is giving me the final answer.

Why does this happen? Does .forEach have a special flag about how it is handled by the interpreter?  
I’ve dug around on the .MDN and I’ve only found that .forEach cannot be chained.

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [January 27, 2017, 1:06am UTC](https://forum.freecodecamp.org/t/dna-pair-algorithm-help-understaning-foreach-behavior/80989/2 "2017-01-27T01:06:21Z")

</div>

Hi. Could you paste your code here instead? Your link only shows the first seven lines of your code.

---

<div class="post-metadata">

**Author:** ![PinkPanther901](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@PinkPanther901](https://forum.freecodecamp.org/u/PinkPanther901)\
**Post date:** [January 27, 2017, 4:21pm UTC](https://forum.freecodecamp.org/t/dna-pair-algorithm-help-understaning-foreach-behavior/80989/3 "2017-01-27T16:21:25Z")

</div>

Sorry about that!

Here’s my code directly, and a jsfiddle that should work [https://jsfiddle.net/InkAnt908/4hzL7us6/1/](https://jsfiddle.net/InkAnt908/4hzL7us6/1/)

```
function pairElement(str){
var arr = [];
var tempArr = [];

//Step 1.
//Take str, push each char to a sub-array,
//Then push that sub-array into the main array & reset
for (var i = 0; i<str.length; i++){
tempArr.push(str[i]);
arr.push((tempArr));
tempArr = [];
}

//Up to this point, we ~should~ have arr = [["G"],["C"],["G"]]
console.log("Sanity check:",arr);

//Step 2.
//Loop through each subarray, add the companion letter to it
arr.forEach(function addPair(arr){
//console.log(arr[j],"is:",arr[j]);
while (arr.length <= 1){
if (arr[0] == "A"){
   arr.push("T");
  }
else if (arr[0] == "T"){
   arr.push("A");
  }
else if (arr[0] == "G"){
   arr.push("C");
  }
else if (arr[0] == "C"){
   arr.push("G");
  }
else{
 console.log("Something is broken :(");
 }
console.log("Add pair done, returning:",arr);
return arr;
}
});

console.log("DONE!",arr);
return arr;
}

pairElement("GCG");
```

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [January 29, 2017, 1:23am UTC](https://forum.freecodecamp.org/t/dna-pair-algorithm-help-understaning-foreach-behavior/80989/4 "2017-01-29T01:23:12Z")

</div>

> [@PinkPanther901](#):
>
> In the middle I have a “sanity check” console log that breaks the two steps in half, but when it runs it is ~actually~ logging as if the .forEach section of the code below it has already run (adding the second letter pair to the sub-array), thus is giving me the final answer.

It doesn’t seem to be the case. Look at this.  
[https://repl.it/FY57/1](https://repl.it/FY57/1)

I think it has something to do with the browser’s console (like, it doesn’t give you a snapshot of what the array looks like before you run `.forEach`, but instead gives you a “link” to the array).

---

<div class="post-metadata">

**Author:** ![PinkPanther901](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@PinkPanther901](https://forum.freecodecamp.org/u/PinkPanther901)\
**Post date:** [January 29, 2017, 1:42am UTC](https://forum.freecodecamp.org/t/dna-pair-algorithm-help-understaning-foreach-behavior/80989/5 "2017-01-29T01:42:53Z")

</div>

Oh wow, that’s interesting!

On that linked module it works as you described, but in the FCC courses, jsfiddle, and a local page with tags it gives me the original result: [http://imgur.com/rcJ0w2r](http://imgur.com/rcJ0w2r)

It does the same thing in Firefox, as well… weird.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:11pm UTC](https://forum.freecodecamp.org/t/dna-pair-algorithm-help-understaning-foreach-behavior/80989/6 "2021-01-16T17:11:55Z")

</div>

Closed on January 16, 2021, 5:11pm UTC


