Skip to content Skip to sidebar Skip to footer

How To Returns Correctly Matched Array?

I have a function that takes a string of DNA and how to return correctly matched dna array The code that I have tried: function checkDNA(dna) { var dnaarr = []; for(var i

Solution 1:

You could take an object for the nucleobases and take the characters of the string for getting the values for each character.

For unknow characters, a later filtering is applied.

function pair(string) {
    var nucleobases = { G: 'C', C: 'G', T: 'A', A: 'T' };

    return Array
        .from(string.toUpperCase(), s => s in nucleobases && s + nucleobases[s])
        .filter(Boolean);
}

console.log(pair('GTTC'));

Solution 2:

You are thinking this in the wrong way. You have to register those values.

Think about in the db way.

Here is an exmaple.

var dnaRules = {}    
/// register DNA
register = function(key) {
var item = {};
       if (dnaRules[key])
           return dnaRules[key];
         else {
          item[key]= [];
          item.equal = function(value){
          item["data"] = value;
          }
          dnaRules[key]= item;
          }
          return item;
} 
register("GGC").equal(['GC', 'GC', 'CG']);
register("gat").equal(["GC", "AT", "TA"]);
register("PGYYYHVB").equal(["GC", "GC", "GC"]);
// Search DNA
search = function(value){
  var key = null;
  Object.keys(dnaRules).forEach(function(v){
 if (v.toLowerCase().indexOf(value.toLowerCase())>=0)
  key = v;
  });
  
  return key== null ? null : dnaRules[key]
}

var item = search("ggc"); // if the item dose not exist you get null
// now display the items 
console.log(item.data)
/// or you could change them even 
// item.equal([])

Post a Comment for "How To Returns Correctly Matched Array?"