Given str="GC" I need an output of:
[ [ 'G', 'C' ], [ 'C', 'G' ] ]
My code below seems to run the first if statement, create the first entry for the "G":
[["G" "C"]]
But then when the second iteration ("C"), which runs the else if, it converts both to
[ [ 'C', 'G' ], [ 'C', 'G' ] ]
I don't understand, the values I assign in the first if statement are overwritten, and I don't understand why.
function pairElement(str) {
let a = []
let b = []
for (let i=0; i<str.length;i++) {
a[i]=b
if (str[i]=="G") {
console.log("case 1")
a[i][0]="G"
a[i][1]="C"
console.log(a)
}
else if (str[i]=="C") {
console.log("case 2")
a[i][0]="C"
a[i][1]="G"
console.log(a)
}
}
return a;
}
console.log(pairElement("GC")); //desired output: [ [ 'G', 'C' ], [ 'C', 'G' ] ]