Mysterious Organism Challenge Project (JavaScript)

Enjoyed this. Includes a solution to the follow-on challenge to identify the two most similar specimens.

1 Like

This is my code, it would be great to get some feedback! :grin:

// 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,
   dna,
   mutate() {

     //Select a random element in the dna array
   let selectElement = Math.floor(Math.random() * array.  
   length);

     //Change the current base to a different one 

    if (selectElement === 'A') {
    this.dna.splice(selectElement, 0, 'T');
  } else if (selectElement === 'T') {
    this.dna.splice(selectElement, 0, 'A');
  } else if (selectElement === 'G') {
    this.dna.splice(selectElement, 0, 'C');
  } else {
    this.dna.splice(selectElement, 0, 'G');
  }
     //Return the object's dna 

return this.dna;
   }, 

   compareDna(secondPaequor) {
     let identicalBases = [];

     //Using one loop only, since we only need to compare 
     //values that are in the same position 

      for (let i = 0; i < this.dna.length; i++) {
        if (this.dna[i] === secondPaequor[i]) {
         identicalBases.push([i]);
        console.log(identicalBases);

      //Take the number of identical bases (the new array 
      //length), divided by the total number of bases (15) 
      //and multiply by 100
      //Use the .toFixed() method to limit the number of 
      //decimal places 
 }
}
   
let percentDna = (identicalBases.length/15 * 100).toFixed();
console.log(percentDna);
return console.log(`Specimen #${this.specimenNum} and Specimen #2 have ${percentDna}% DNA in common.`);
    },

    willLikelySurvive() {
      //Check every element of the dna array and add any C 
      //or G elements to a new array
      let cGBases = [];
      for (let i= 0; i < this.dna.length; i++) {
        if (this.dna[i] === 'C' || this.dna[i] === 'G') {
          cGBases.push([i]);
        } 
      }
      if (cGBases.length > 8) {
        return true;
      } else {
        return false;
      }
   //60% of 15 is 9
   //If the array.length is > 8 return true (since arrays start counting at 0)
   //Otherwise return false 
    }
   }
  };

//Use the factory f(x) to create 30 instances of pAequor
//New empty array 
let paequorArray = [];  
//Specimen Number Counter
let specimenNumCounter = 1;

while (paequorArray.length < 30) {
  //Call the factory function with the parameters
let newPaequor = pAequorFactory(specimenNumCounter, mockUpStrand());
//Call the willSurvive method and if it returns true, add it to the array 
if (newPaequor.willLikelySurvive()) {
   paequorArray.push(newPaequor);
}
//Increment the specimen number counter
  specimenNumCounter++;
}

console.log(paequorArray);



Hi all, just finished this assignment, took me quite a while.

I could really use some pointers whether I’ve used the reduce method correctly and whether it will, in fact, return the maximums result in the array of objects. Thanks in advance to any who takes the time to take a look. Much love

I’m having a serious doubts about my mutate function. The entire code can be found here

But the mutate function in particular is eating at my brain. My problem is it generally works but it appears my check for if the pairs are different just doesn’t work at all. About a quarter of the time it just doesn’t mutate anything.

Here’s the code:

mutate () {
          const index = Math.floor(Math.random() * this.dna.length);
          let oldBase = this.dna[index];
          let newBase = '';
          //ensures the base is different NOT WORKING
          do {
            newBase = returnRandBase();
          } 
          while (newBase === oldBase);
          this.dna[index] = newBase;
        }

My do…while appears to just not do anything.

Any Ideas?