Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created January 12, 2021 23:27
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/8159886d9f733b66a875d5158b2c7a53 to your computer and use it in GitHub Desktop.
Save codecademydev/8159886d9f733b66a875d5158b2c7a53 to your computer and use it in GitHub Desktop.
Codecademy export
const dnaBases = ['A', 'T', 'C', 'G'];
// Returns a random DNA base
const returnRandBase = () => {
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 = (specimenNum, dna) => {
return {
_specimenNum: specimenNum,
_dna: dna,
get specimenNum() { return this._specimenNum; },
get dna() { return this._dna; },
set dna(array) { this._dna = array; },
mutate() {
const dnaIndex = Math.floor(Math.random() * 15);
do {
newDNA = dnaBases[Math.floor(Math.random() * 4)];
} while (dna[dnaIndex] === newDNA);
dna[dnaIndex] = newDNA;
return dna;
},
compareDNA(pAequor) {
let numMatches = 0;
for (let i = 0; i < 15; i++)
{
if (pAequor.dna[i] === dna[i])
numMatches++
}
const percentPerMatch = 1 / 15;
const matchPercent = numMatches * percentPerMatch;
console.log(`specimen #${specimenNum} and specimen #${pAequor.specimenNum} have ${(matchPercent * 100).toFixed(2)}% DNA in common`);
},
willLikelySurvive() {
let numMatches = 0;
for (let i = 0; i < 15; i++)
{
if (['C', 'G'].includes(dna[i]))
numMatches++
}
const percentPerMatch = 1 / 15;
const matchPercent = numMatches * percentPerMatch;
return matchPercent >= .6;
}
}
};
const getPAequorLikelySurviveArray = () => {
const pAequors = [];
for (let i = 0; i < 30; i++) {
let pAequor = pAequorFactory(i + 1, mockUpStrand());
while (!pAequor.willLikelySurvive())
pAequor.dna = pAequor.mutate();
pAequors.push(pAequor);
}
return pAequors;
}
const pAequors = getPAequorLikelySurviveArray();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment