Skip to content

Instantly share code, notes, and snippets.

@ManuSan09
Created January 9, 2021 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ManuSan09/d032c5eb2be88e2534fd4d2d0df6d5c7 to your computer and use it in GitHub Desktop.
Save ManuSan09/d032c5eb2be88e2534fd4d2d0df6d5c7 to your computer and use it in GitHub Desktop.
My solution for Mysterious Organism
// 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 = (number, array) =>{
return {
specimenNum: number,
dna: array,
//Change some bases of the DNA
mutate (){
const randomNum = Math.floor(Math.random() * array.length);
let randomBase = returnRandBase();
if (this.dna[randomNum] === randomBase){
let randomBase = returnRandBase();
this.dna[randomNum] = randomBase;
} else {
this.dna[randomNum] = randomBase;
}
return this.dna;
},
//Compare two instances
compareDNA (pAequor){
let commonDNA = 0;
let j = 0;
for (dna in this.dna){
if (this.dna[dna] === pAequor.dna[j]){
commonDNA ++;
}
j ++;
}
const percentage = ((commonDNA/this.dna.length) * 100).toFixed();
return `specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${percentage}% DNA in common`
},
//Method for the chance of survival
willLikelySurvive (){
let timesOfGOrC = 0;
for (dna in this.dna){
if (this.dna[dna] === "G" || this.dna[dna] === "C"){
timesOfGOrC ++;
}
}
const percentage = (timesOfGOrC/(this.dna.length)*100).toFixed();
if (percentage >= 60){
return true;
} else {
return false;
}
},
//Complementary DNA strand
complementStrand (){
for (base in this.dna){
if (this.dna[base] === "A"){
this.dna[base] = "T";
} else if (this.dna[base] === "G"){
this.dna[base] = "C";
} else if (this.dna[base] === "T"){
this.dna[base] = "A";
} else if (this.dna[base] === "C"){
this.dna[base] = "G";
}
}
return this.dna;
}
}
};
//Creating 30 instances
let i = 1;
let arrayOfSurvive = [];
while (i < 31){
let instance = pAequorFactory(i, mockUpStrand());
if (instance.willLikelySurvive() === true){
arrayOfSurvive.push(instance)
}
i ++;
}
//Check the survive instances
console.log(arrayOfSurvive);
//Check the complementStrand
/*let complementStrandProof = pAequorFactory(1, mockUpStrand());
console.log(complementStrandProof.dna);
console.log(complementStrandProof.complementStrand())
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment