Skip to content

Instantly share code, notes, and snippets.

@SaimAli5
Created November 28, 2022 18:22
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 SaimAli5/8e12270d6d9222bc5f17c968c3f4731e to your computer and use it in GitHub Desktop.
Save SaimAli5/8e12270d6d9222bc5f17c968c3f4731e to your computer and use it in GitHub Desktop.
Challenge Project: Mysterious Organism
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
};
// Factory Function
const pAequorFactory = (num, arr) => {
const organism = {
specimenNum: num,
dna: arr,
mutate() {
const randomBase = this.dna[Math.floor(Math.random() * this.dna.length)];
const newBase = this.dna.filter((arr) => {
if (arr[Math.floor(Math.random() * this.dna.length)] === "A") {
let notA = ["T", "C", "G"];
return (arr[randomBase] = notA[Math.floor(Math.random() * notA.length)]);
} else if (arr[Math.floor(Math.random() * this.dna.length)] === "T") {
let notT = ["A", "C", "G"];
return (arr[randomBase] = notT[Math.floor(Math.random() * notT.length)]);
} else if (arr[Math.floor(Math.random() * this.dna.length)] === "C") {
let notC = ["A", "T", "G"];
return (arr[randomBase] = notC[Math.floor(Math.random() * notC.length)]);
} else if (arr[Math.floor(Math.random() * this.dna.length)] === "G") {
let notG = ["A", "T", "C"];
return (arr[randomBase] = notG[Math.floor(Math.random() * notG.length)]);
} else {
return "ERROR";
}
});
return newBase;
},
compareDNA (paObj) {
const identicalBases = this.dna.filter((val, i) => paObj[i] === val );
const identicalBasesPercentage = identicalBases.length/this.dna.length * 100
return `specimen #1 and specimen #2 have ${identicalBasesPercentage.toFixed()}% DNA in common.`
},
willLikelySurvive() {
let cdCount = 0;
this.dna.forEach(element => {
if (element === 'C' || element === 'G') {
cdCount += 1;
}
});
const cdBases = cdCount/this.dna.length * 100;
if(cdBases > 60){
return true
} else {
return false
}
}
};
return organism;
};
const paSurvivors = []
let idCounter = 1
while(paSurvivors.length < 30){
let newOrg = pAequorFactory(idCounter, mockUpStrand());
if (newOrg.willLikelySurvive()) {
paSurvivors.push(newOrg);
}
idCounter++;
}
console.log(paSurvivors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment