PhotoNerd
PhotoNerd

Reputation: 31

JavaScript for loop confusion

I'm writing what I think is a straightforward for loop but it is not behaving the way I want it to. I want to understand why it's doing what it's doing:

function pair(str) {

  var finalArray = [];
  var pushArray  = [];
  
  var lookup = {
    G: "C",
    C: "G",
    A: "T",
    T: "A"
  };
  
  for (i=0; i<str.length; i++) {
    pushArray[0] = str[i];
    pushArray[1] = lookup[str[i]];
    finalArray.push(pushArray);
  }
  return finalArray;
}

pair("ATCGG");

I want it to return [["A","T"],["T","A"],["C","G"],["G","C"],["G","C"]]

What I'm actually getting is [["G","C"],["G","C"],["G","C"],["G","C"],["G","C"]]

It seems all the ["G","C"] is for where i = 4. Why is my code not looping? What am I missing?

Upvotes: 3

Views: 128

Answers (6)

gavgrif
gavgrif

Reputation: 15489

try this - noticing the original post was to determine the base pair sequence in DNA code. I offer the following. The key with the bases array (lookup in the OP) is knowing that each genetic base can only partner with a predetermined other base to give the following combinations (G-C, C-G, A-T, T-A). Therefore the bases array can be simplified, and then it is simple matter of iterating through the passed string, finding the first index of that particular base in the bases array and matching it to the next one (to make a base-pair). Then just pushing the pair into the DNA sequence array. Note that I am "console.logging" it - to demonstrate that this works - rather than using "return".

Knowledge of genetics can be useful even in programming :)

pair("ATCGG");

function pair(str) {
  var DNAsequence = [];
  var bases = ["G","C","G","A","T","A"];

  for (i=0; i<str.length; i++) {
      var firstBase=bases.indexOf(str[i]);
      var secondBase=firstBase+1;
      var basePair=[bases[firstBase],bases[secondBase]];

    DNAsequence.push(basePair);
  }
  console.log( DNAsequence);
}

and the console.log show the following, as requested:

[["A","T"], ["T","A"], ["C","G"], ["G","C"], ["G","C"]]

Upvotes: 0

Barmar
Barmar

Reputation: 780724

The problem is that finalArray.push(pushArray) doesn't make a copy of pushArray. Each time you do this, you're pushing a reference to the same array, which you then modify on the next iteration. You need to create a new array each time.

function pair(str) {

  var finalArray = [];
  var pushArray;
  
  var lookup = {
    G: "C",
    C: "G",
    A: "T",
    T: "A"
  };
  
  for (var i=0; i<str.length; i++) {
    pushArray = [];
    pushArray[0] = str[i];
    pushArray[1] = lookup[str[i]];
    finalArray.push(pushArray);
  }
  return finalArray;
}

Upvotes: 5

ProllyGeek
ProllyGeek

Reputation: 15836

Your assumptions are partially right , except that everytime you were pushing a reference to same variable.

This should work :

function pair(str) {

  var finalArray = [];


  var lookup = {
    G: "C",
    C: "G",
    A: "T",
    T: "A"
  };

  for (i=0; i<str.length; i++) {
  var pushArray=[];
    pushArray[0] = str[i];
    pushArray[1] = lookup[str[i]];
    finalArray.push(pushArray);
  }
  console.log(finalArray.toString())
  return finalArray;
}

pair("ATCGG");

Fiddle

Upvotes: 0

Fabius
Fabius

Reputation: 528

Simply replace this:

finalArray.push(pushArray);

With this:

finalArray = finalArray.concat(pushArray);

Upvotes: 0

takashi
takashi

Reputation: 1

In your code, the pushArray is a single instance object and it is over written with new values every time in the for loop. You can create separated object instance for each str-lookup pair to modify your code as below.

function pair(str) {

  var finalArray = [];
  
  var lookup = {
    G: "C",
    C: "G",
    A: "T",
    T: "A"
  };
  
  for (i=0; i<str.length; i++) {
    finalArray.push([ str[i], lookup[str[i]] ]);
  }
  return finalArray;
}

var res = pair("ATCGG");
alert(JSON.stringify(res));

hope this help you.

Upvotes: 0

Mulan
Mulan

Reputation: 135197

This should help you :)

function pair(str) {      
  var lookup = {
    G: 'C',
    C: 'G',
    A: 'T',
    T: 'A'
  };
  return str.split('').reduce(function(ys,x) {
    return ys.concat([[x, lookup[x]]]);
  }, []);
}

var result = pair('ATCGG');
console.log(JSON.stringify(result));
// [["A","T"],["T","A"],["C","G"],["G","C"],["G","C"]]

Upvotes: 3

Related Questions