• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

151
Views
How do i carry function result back up functions to reiterate over the functions again?

I have an Array that is passed through a forEach loop. Each item is then passed through and is checked for a specific order or characters. If it is true the characters are meant to be removed from the string, then it is supposed to be looped back over with the new string. (Old string minus the checked characters)

My problem is the fact that all the functions are still locally holding the old String (Before the characters were removed). How can I pass the new string into the functions without resetting the value "deleteStringData" currently holds?

let dnaArray = ["ACATATAGACATACGT","AAAAAATACATAGTAGTCGGGTAG","ATACATCGGGTAGCGT"];
dnaStrand = "";



//SORT THROUGH EACH ITEM IN ARRAY
dnaArray.forEach((dnaStrand, index) => {
  if (findDna(dnaStrand)) {
    console.log("Case #" + index + " " + dnaStrand + ": YES");
}
  else {
      console.log("Case #" + index + " " + dnaStrand + ": NO");
  };
});






function findDna(dnaStrand){
  if (findHead(dnaStrand)){
    if(findBody(dnaStrand)){
      console.log("dna");
      return true;
    }
  }
  else {
    return false;
  }
};


function findHead(dnaStrand){
  if (findGlobe(dnaStrand)){
    if (findEyeSpots(dnaStrand)) {
      return true;
    }
  }
  else{
    return false;
  }
};



function findBody(dnaStrand){
  if (findGlobe(dnaStrand) && findLegs(dnaStrand)) {
    return true;
  }
  else {
    return false;
  }
};



function findGlobe(dnaStrand){
  if(findMatch(dnaStrand, /(A+(TAC|CAT)A)/)){
    return true;
  }else{
    console.log("No Globe");
  }
};



function findEyeSpots(dnaStrand){
  if(findMatch(dnaStrand, /T(CG*T)*AG/)){
    return true;
  }else{
    console.log("No Eyes");
  }
};

function findLegs(dnaStrand){
  if(findMatch(dnaStrand, /CG*T/)){
    return true;
  }else{
      console.log("No Legs");
  }
};


function findMatch (dnaStrand, regex) {
  dnaStrand = String(dnaStrand);
  let isMatch = dnaStrand.match(regex);
  isMatch = String(isMatch[0]);
  //console.log(isMatch);
  if (isMatch) {
    deleteStringData(dnaStrand, isMatch);
    return true;
  }
  else {
    return false;
  }
};

function deleteStringData (dnaStrand, string) {
   dnaStrand = dnaStrand.replace(string, "");
};

about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Sorry, but I am sort of busy and don't have the time to read through all your code. Based on your question, I can give you some basic guidance.

make sure your function takes a parameter. If each time the end result is not satisfactory or you just want to repeat it again, do an if statement, and if you're going to repeat it, do yourFunction(endresult) inside. If you worry about lag, consider making your end result a global variable and setting a setTimeout to your function.

about 3 years ago · Juan Pablo Isaza Report

0

If you declare the test functions inside the loop you can act on single instance of the strand in that scope without needing to pass any references around.

You can also use Promises to chain the tests.

This may not be the most efficient method but it is very readable.

(async () => {
  const strands = [
    'ACATATAGACATACGT',
    'AAAAAATACATAGTAGTCGGGTAG',
    'ATACATCGGGTAGCGT'
  ];

  const results = [];

  async function analyze(strand, index) {
    let result = {
      'case' : parseInt(index), strand
    };

    function findMatch(pattern) {
      return new Promise((resolve, reject) => {
        let m = strand.match(pattern)[0];

        if (m) {
          strand = strand.replace(m, ''); 
          resolve(true);
        } else {
          reject();
        }
      });
    }

    function findHead() {
      return findGlobe().then(findEyes);
    }

    function findBody() {
      return findGlobe().then(findLegs);
    }

    function findGlobe() {
      return findMatch(/(A+(TAC|CAT)A)/);
    }

    async function findEyes() {
      result.eyes = await findMatch(/T(CG*T)*AG/);
    }

    async function findLegs() {
      result.legs = await findMatch(/CG*T/);
    }  

    function found() {
      result.dna = true;
    }

    function failed() {
      result.dna = false;
    }

    function done() {
      results.push(result);
    }

    await Promise.all([
      findHead(),
      findBody()
    ]).then(found)
    .catch(failed)
    .finally(done);
  }

  for (let i in strands) {
    await analyze(strands[i], i);
  }
  
  console.log(results);
})();

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error