*Intermediate Algorithm Scripting: DNA Pairing


I am lost. What may I do in here?

function pairElement(str) {
     let dna={
    
         "A": "T",
    
         "C": "G",

          "T":"A",

          "G": "C"
    
     }
     

}

pairElement("GCG");

the input GCG, should return [[β€œG”, β€œC”], [β€œC”,β€œG”],[β€œG”, β€œC”]]
the input ATA, should return [[β€œA”, β€œT”], [β€œT”,β€œA”],[β€œA”, β€œT”]]

You will need to iterate through all chars in the string to find and save each pair in an Array.

1 Like

I have already the whole object in Object.entries(entriesBasePairs), but I want only the keys which are asked. I have already seperate it, but how can I use everyone of them to find the value key in the object?

function pairElement(str) {
  let entriesBasePairs={

    A:"T",

    T:"A",

    C:"G",

    G:"C"
  
  }

let entriesBase =str.split("");


 //Object.entries(entriesBasePairs); 

for (let [key, value] of Object.entries(entriesBasePairs)){

//console.log(`${key}: ${value}`);


 }
}

pairElement("GCG");
/*
 

*/

I have already used forEach() and I want to find the values keys and I want to push them in the new array

function pairElement(str) {
  let entriesBasePairs={

    A:"T",

    T:"A",

    C:"G",

    G:"C"

  }
let newArr=[];

let entriesBase =str.split("");

let a =entriesBase.forEach(element => newArr.push(entriesBasePairs))

console.log(a)

}
pairElement("GCG");

it is done!. Thanks!

function pairElement(str) {
  let entriesBasePairs={

    A:"T",

    T:"A",

    C:"G",

    G:"C"
  
  }
let newArr=[];
let entriesBase =str.split("");
 //Object.entries(entriesBasePairs); 
for(let i=0;i<entriesBase.length;i++)

for (let [key, value] of Object.entries(entriesBasePairs)){
 
        if(entriesBase[i]==key){

             newArr.push([key, value])    
//console.log(`${key}: ${value}`);

}
}
console.log(newArr)
return newArr;
}
pairElement("GCG");

/*
let a =entriesBase.forEach(element => .push(entriesBasePairs))

console.log(a)*/ 
1 Like

that’s a good solution.
remember, you can iterate string chars without splitting the string

for (let c of str) { /* do something with each char c */}
OR
for (let i=0; i<str.charAt(i); i++) { /* do stuff */}

also since you have an object, you can push its content directly without the extra if statement.

for (let c of str) 
  arr.push([ c, entriesBasePairs[c] ]);
1 Like

I am thankful for your corrections!

1 Like