Skip to content

Instantly share code, notes, and snippets.

@eduairet
Created March 31, 2021 21:05
Show Gist options
  • Save eduairet/633509fff469b6701530fe225e2c499c to your computer and use it in GitHub Desktop.
Save eduairet/633509fff469b6701530fe225e2c499c to your computer and use it in GitHub Desktop.
Mysterious Organism Challenge Project (JavaScript)
// 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;
};
class pAequorFactory {
constructor (num, dna) {
this.specimenNum = num;
this.dna = dna;
this.dnaSurvive = null;
this.compDNA = [];
}
mutate () {
const baseIndex = Math.floor(Math.random() * this.dna.length)
let baseToChange = this.dna[baseIndex];
let mutation = returnRandBase();
while (mutation === baseToChange) {
mutation = returnRandBase();
}
this.dna[baseIndex] = mutation;
}
compareDNA (pAequor) {
let compareArr = [];
let equalCount = 0;
for (let i=0; i < this.dna.length; i++) {
if (this.dna[i] === pAequor.dna[i]) {
compareArr.push(true);
equalCount += 1;
} else {
compareArr.push(false);
}
}
const equalPercentage = equalCount/this.dna.length*100
console.log(`Specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${equalPercentage.toFixed(2)}% in common`);
return equalPercentage;
}
willLikelySurvive () {
let cOrGCount = 0;
for (const el of this.dna) {
if (el === 'C' || el === 'G') {
cOrGCount += 1;
}
}
if (cOrGCount/this.dna.length > .6) {
this.dnaSurvive = true;
} else {
this.dnaSurvive = false;
}
}
complementStrand () {
for (const el of this.dna) {
if (el === 'A') {
this.compDNA.push('T')
} else if (el === 'T') {
this.compDNA.push('A')
} else if (el === 'C') {
this.compDNA.push('G')
} else if (el === 'G') {
this.compDNA.push('C')
}
}
}
}
let dnaSamples = [];
let bestMAtch = 0;
for (let i=0; i<30; i++) {
dnaSamples[i] = new pAequorFactory(i + 1, mockUpStrand());
dnaSamples[i].willLikelySurvive();
}
for (dnaSample of dnaSamples) {
if (!dnaSample.dnaSurvive) {
while (!dnaSample.dnaSurvive) {
dnaSample.mutate();
dnaSample.willLikelySurvive();
}
}
dnaSample.complementStrand();
dnaSamples.forEach(el => {
if (el.dna !== dnaSample.dna) {
if (dnaSample.compareDNA(el) > bestMAtch) {
bestMAtch = dnaSample.compareDNA(el);
}
}
})
}
console.log(`The most related instances match ${bestMAtch.toFixed(2)}%`);
console.log(dnaSamples);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment