Skip to content Skip to sidebar Skip to footer

When I Access To A Key, I Want Its Value And Set It In An Array. The Access Is By Using Strings

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing When I access to a key, I want its value and set it array.

Solution 1:

Although I'm not exactly sure on what you want to do, this does give the output you're expecting.

functionpairElement(str) {
    const dna = {
        "A": "T",
        "C": "G",
        "T": "A",
        "G": "C"
    }
    let arr = [];
    for (const key of str){
        arr.push([key, dna[key]])
    }
    return arr;
}

// some input testsconsole.log(pairElement("GCG"));
console.log(pairElement("ACTG"));
console.log(pairElement("CCGAT"));

Or a one liner:

functionpairElement(str) {
    const dna = {
        "A": "T",
        "C": "G",
        "T": "A",
        "G": "C"
    }
    
    return str.split('').map(key => [key, dna[key]])
}

// some input testsconsole.log(pairElement("GCG"));
console.log(pairElement("ACTG"));
console.log(pairElement("CCGAT"));

Note that you can iterate through string in Javascript :)

Solution 2:

I think you'll want to:

1) Turn the incoming string into an array using split (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)

2) Turn each element in that array into its array using map (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) and build the pair you're looking for.

Solution 3:

Here you go

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

  let indi = str.split('');
  var dna_arr = [];
  for(var x=0; x<indi.length; x++) {

    varchar = indi[x];
    dna_arr.push(char);
  }

  return dan_arr;



}

pairElement("GCG");

Post a Comment for "When I Access To A Key, I Want Its Value And Set It In An Array. The Access Is By Using Strings"