Mysterious Organism Challenge Project (JavaScript)

Hi all,

Here is my completed attempt at the Mysterious Organism Challenge. Please feel free to give any feedback.

Hello all,

I just completed this project and would like to share to it with you all in order to get any feedback.

Git Hub Mysterious Organism Repository

Heres my project,
I really struggled at task #7 where we had to create 30 new instances of the object, still not sure if I did it right. I wish I had a way to name each function, but I am not sure what is the “industry standard”. Regardless here it is!

Gist:

Very very late reply on my part, but you were right! Each object ended up having the same specimenNum value and as I continued my progress in the full stack course it kind of haunted me and made me want to go back and fix it so I did! I decided to avoid printing the functions in the original object given by the factory function by creating another factory function within the “pAequorParty” function that just utilizes specimen number and dna sequence of the objects that passed the survivability check and created a new object that gets pushed to the “partyArr”. Let me know what you think and thank you for your input! It helped me think about what I wanted my code to do and I’m a lot happier with how it turned out :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 strand of DNA containing 15 bases const mockUpStrand = () => { const newStrand = []; for (let i = 0; i < 15; i++) { newStrand.push(returnRandBase()); } return newStrand; }; const pAequorFactory = (num, dnaBaseArr) => { return { specimenNum: num, dna: dnaBaseArr, mutate() { let dnaSelector = Math.floor(Math.random() * this.dna.length); let baseSelect = this.dna[dnaSelector]; if (baseSelect !== returnRandBase()) { this.dna.splice(dnaSelector, 1, returnRandBase()); return this.dna } else { this.mutate(); } return this.dna; }, compareDNA(obj) { let similarDNA = 0; for (let i = 0; i < obj.dna.length; i++){ if (this.dna[i] === obj.dna[i]){ similarDNA++ } }; let percentage = (similarDNA / 15 * 100).toFixed(2); console.log(`The percentage of identical DNA between the two given specimen is ${percentage}%`); }, willLikelySurvive() { let survivalChance = 0 for (let i=0; i<this.dna.length; i++){ if (this.dna[i] === 'C' || this.dna[i] === 'G'){ survivalChance++ } } if (survivalChance >= 9){ return true } return false }, }; }; let pAequor = pAequorFactory(0, mockUpStrand()); const pAequorParty = (obj) => { const survivorFactory = (num, dna) => { return { specimenNum: num, dna: dna, }; }; let partyArr = []; while (partyArr.length <30) { if(obj.willLikelySurvive() === true){ obj.specimenNum++ partyArr.push(survivorFactory(obj.specimenNum, obj.dna)); } obj.mutate() } return partyArr; }; let testSubj= pAequorParty(pAequor); console.log(testSubj);

Hello, just completed the project and want to share this!

Hi everyone. Here’s my solution for this project:

https://gist.github.com/ecad48b9e4550cd3179b5188ae7e296a

Any advice on how to improve my code?
I’m open to all feedback! :smiley:

In case anyone would like to check out the way I have completed this I have created a repository found in the link below with an explanation of the code. :grinning:

But overall I found this one a lot more simple and more relaxing after completing the credit card challenge!

GitHub challenge link: mysterious-organism-challenge/main.js at main · DEVArt1st/mysterious-organism-challenge · GitHub

Here is how I solved this challenge:

Hello Everyone,

This one was not an easy one for me. Thankfully, after a lot of research, I was able to finish. Please I would love to hear your feedback:

// Returns a random DNA base
const returnRandBase = () => {
  const dnaBases = ["A", "T", "C", "G"];
  return dnaBases[Math.floor(Math.random() * 4)];
};

// Returns a random single strand of DNA containing 15 bases
const mockUpStrand = () => {
  const newStrand = [];
  for (let i = 0; i < 15; i++) {
    newStrand.push(returnRandBase());
  }
  return newStrand;
};

let pAequorFactory = (num, arr) => {
  return {
    _specimenNum: num,
    _dna: arr,

    //randomly selecting a base in the dna  and changing the current base to a different base.
    mutate() {
      for (let i = 0; i < this._dna.length; i++) {
        let temp = returnRandBase();
        while (this._dna[i] === temp) {
          temp = returnRandBase();
        }
        this._dna[i] = temp;
      }
      return this._dna;
    },

    //compare the current dna with the passed in dna and compute how many bases are identical and in the same locations.
    compareDNA(obj) {
      let counter = 0;
      for (let i = 0; i < this._dna.length; i++) {
        if (this._dna[i] === obj._dna[i]) {
          counter++;
        }
      }
      console.log(
        `specimen #${this._specimenNum} and specimen #${
          obj._specimenNum
        } have ${(counter / 15) * 100}% DNA in common`
      );
    },

    //returns true if the dna array contains at least 60% 'C' or 'G' bases.
    willLikelySurvive() {
      let c = 0;
      let g = 0;
      for (let i = 0; i < this._dna.length; i++) {
        if (this._dna[i] === "C") {
          c++;
        } else if (this._dna[i] === "G") {
          g++;
        }
      }
      return c / 15 >= 0.6 || g / 15 >= 0.6;
    },
  };
};

//create 30 instances of pAequor that can survive in their natural environment. Store these instances in an array
const pAequor = [];
for (let counter = 1; counter <= 30; counter++) {
  let temp = pAequorFactory(counter, mockUpStrand());
  while (temp.willLikelySurvive()) {
    temp.mutate();
  }
  pAequor.push(temp);
}

This is my solution for this Challenge.