Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created December 1, 2021 09:36
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 codecademydev/2251e70a755d4880316bd2f1c9721b86 to your computer and use it in GitHub Desktop.
Save codecademydev/2251e70a755d4880316bd2f1c9721b86 to your computer and use it in GitHub Desktop.
Codecademy export
// 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
}
//factory function to create a specimen
const pAequorFactory = (specimenNum, dna) => {
return {
specimenNum,
dna,
mutate() {
const randNum = Math.floor(Math.random()*this.dna.length);
let giveNewBase = letter => {
let newBase;
do {
newBase = returnRandBase();
//console.log(newBase);
} while (newBase === letter);
//console.log(letter, newBase);
return newBase;
};
this.dna[randNum] = giveNewBase(this.dna[randNum]);
},
compareDNA(anotherObj) {
let haveInCommon = 0;
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === anotherObj.dna[i]) {
haveInCommon += 1;
//console.log(i, this.dna[i], haveInCommon);
}
}
console.log(`specimen #${this.specimenNum} and specimen #${anotherObj.specimenNum} have ${(haveInCommon/15*100).toFixed()}% DNA in common.`);
},
willLikelySurvive() {
let baseCorG = 0;
for (let element of this.dna) {
if (element === 'C' || element === 'G') {
baseCorG++;
}
}
//console.log(baseCorG);
return (baseCorG/15*100).toFixed() > 60;
}
}
}
//test the function factory
//const testObject1 = pAequorFactory(0,mockUpStrand());
//const testObject2 = pAequorFactory(1,mockUpStrand());
//console.log(testObject1);
//testObject.mutate();
//console.log(testObject2);
//testObject1.compareDNA(testObject2);
//console.log(testObject1.willLikelySurvive());
const superpAequor = [];
let i = 0;
while (i < 30) {
const tryObj = pAequorFactory(i, mockUpStrand());
if (tryObj.willLikelySurvive()) {
superpAequor.push(tryObj);
i++;
}
}
//console.log(superpAequor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment