Mysterious Organism - array of 30 (me again!)

Hi all
Me again, getting good use out of this forum today!
I am working on part 7 of mysterious organism.
Here’s my code below… with part 7 starting at line 81.
I hope you can see that I’ve tried to break the problem into steps, with a while loop to execute until the arrSurvivors has 30 elements.
It could be I’m getting stuck on how to increase ‘num’ each time.
That or the actual generation of each pAequor.
I’ve put let num=0 inside the while loop but obviously that just resets num to 0 each time it loops round.
Please could I request some assistance?

// Returns a random DNA base
const returnRandBase = () => {
    const dnaBases = ['A', 'T', 'C', 'G']
    return dnaBases[Math.floor(Math.random() * 4)] 
  }
  
  // Returns a random single stand of DNA containing 15 bases
const mockUpStrand = () => {
    const newStrand = []
    for (let i = 0; i < 15; i++) {
      newStrand.push(returnRandBase())
    }
    return newStrand
  }

 
const pAequorFactory = (specimenNum, dna) => {
    return {
        specimenNum: specimenNum,
        dna: dna,            

//mutate -METHOD-
        mutate () {
            const randIndex = Math.floor(Math.random() * this.dna.length);
            let newBase = returnRandBase();

//to compare the dna base at the random index with newBase (a randomly generated base), and if they're the same, redo the step until they're not the same.            
            while (this.dna[randIndex] === newBase) {
                newBase = returnRandBase();
              }
//reassign the randIndex base as the newBase
              this.dna[randIndex] = newBase;
              return this.dna;
            },   
        

//compare DNA -METHOD-
// takes in obj as an argument (which is another pAequor object).  Compare the .dna of the passed in object with the .dna of the current pAequor

        compareDNA (obj) {

//locate the obj's .dna
          let comparativeDNA = obj.dna;
//empty array to take common DNA
          let commonDNA = [];
        
//compare the two arrays, push common elements to commonDNA array
for (let i=0; i<comparativeDNA.length; i++) {
  if (comparativeDNA[i] === this.dna[i]) {
    commonDNA.push(comparativeDNA[i])
  }
}

//divide length of new array by the length of the DNA array, *100 = the % of DNA in common
          let pcentCommonDNA = ((commonDNA.length / comparativeDNA.length) * 100)

          console.log(`The pAqeuor ${obj.specimenNum} and the pAequor ${this.specimenNum} have ${pcentCommonDNA}% of DNA in common`);
        },


//willLikelySurvive -METHOD-
//calculates % of DNA that is C or G, and if greater than 60%, returns true
        willLikelySurvive () {

          const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
          let numOfC = (countOccurrences(this.dna, 'C'));
          let numOfG = (countOccurrences(this.dna, 'G'));
          let numofCG = numOfC + numOfG;
          
          let decCG = (numofCG / this.dna.length);

          if (decCG >= 0.6 ) {
            console.log(true);
          } else {
            console.log(false);
          }
        },
}
};

// execute pAequor factory 30 times infinitely until you populate an array with 30 that have will likely survive = true
  let arrSurvivors = [];
  let num = 0;
//need a loop that will execute until the array has 30 elements
while (arrSurvivors.length < 30) {


//function to execute the pAeqor factor with an increasing specimen num each time.
//generate a pAequor

let organism = pAequorFactory(num, mockUpStrand());

//push survive=true arrays to arrSurvivors
 if (organism.willLikelySurvive() === true) {
   arrSurvivors.push(organism)
 };
//increase num by 1 each time
 num ++;
 return num;
};

console.log(arrSurvivors);

1 Like

Your willLikelySurvive method codes not return anything;
instead, it has

          if (decCG >= 0.6 ) { 
            console.log(true); 
          } else { 
            console.log(false); 
          }

You may need to replace console.log with return in order for the .willLikelySurvive() to do what you want it to do when you use .willLikelySurvive() in this:

 if (organism.willLikelySurvive() === true) { 
   arrSurvivors.push(organism) 
 };

Also, I don’t understand why you have

return num;

in that while-loop.

1 Like