DNA Pairing - problem with pushing to the array form Object.values

DNA Pairing - problem with pushing to the array form Object.values
0.0 0

#1

There is a problem with my code when pushing to the array.
The problem is with str[i]. If I replace str[i] with “A”, “T” etc. it works as I expect it to work.
Why isn’t js reading str[i] ?

function pairElement(str) {
  var pairs = {
    A: ["A", "T"],
    T: ["T", "A"],
    C: ["C", "G"],
    G: ["G", "C"]
  };
  var paired = [];
  str = str.split("");
  
  for (i = 0; i < str.length; i++){
    paired.push(Object.values(pairs.str[i]));
  }
  return paired;

}

pairElement("GCG");

Your browser information:

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

Link to the challenge:


#2

pairs.A works because A is the actual key. If you use str[i] it will search for a key named “str” and then it will try to get the second item in that object/array.

You are confusing dot notation with bracket notation. With dot notation (e.g. pairs.A) it will get the value whose key is A. In your case that would be ["A", "T"]. So, dot notation takes A literally.

Now, when you use bracket notation (e.g. pairs[x]) it will try and find a variable x get its value and search for that:

var x = "A";
console.log(pairs[x]); // logs: ["A", "T"]

pairs[x] === pairs["A"] === pairs.A