Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 24, 2020 13:14
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/b4218115df60b764c02bbd7862165a67 to your computer and use it in GitHub Desktop.
Save codecademydev/b4218115df60b764c02bbd7862165a67 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
}
const pAequorFactory = (nr, dnaArray) => {
return {
specimenNum: nr,
dna: dnaArray,
mutate() {
const dnaBases = ['A', 'T', 'C', 'G'];
const randIndex = Math.floor(Math.random() * 15);
const selectedBase = this.dna[randIndex];
const newBases = dnaBases.filter(base => base !== selectedBase);
this.dna[randIndex] = newBases[Math.floor(Math.random() * 3)];
return this.dna;
},
compareDNA(pAequor) {
const ex1 = this.dna;
const ex2 = pAequor.dna;
let sameBases = 0;
for (let i = 0; i < ex1.length; i++) {
if (ex1[i] === ex2[i]) {
sameBases++;
}
}
const percentageSame = Math.round((sameBases / 15) * 100);
console.log(`Speciment #${this.specimenNum} and #${pAequor.specimenNum} have ${percentageSame}% DNA in common.`);
},
willLikelySurvive() {
const cgArray = this.dna.filter(base => base === 'C' || base === "G");
return (cgArray.length / 15) >= 0.6 ? true : false;
}
}
}
let surviveNr = 1;
let pAequorArray = [];
while (surviveNr <= 30) {
const newPAequor = pAequorFactory(surviveNr, mockUpStrand());
if (newPAequor.willLikelySurvive()) {
pAequorArray.push(newPAequor);
surviveNr++;
}
}
//Tests
console.log('Length should be 30. Result: ' + pAequorArray.length);
console.log('Array of specimens:');
pAequorArray.forEach(specimen => console.log(specimen.dna));
console.log('Percentage of C/G in each specimen: ');
pAequorArray.forEach(specimen => {
const cgArray = specimen.dna.filter(base => base === 'C' || base === 'G');
const percentageCG = Math.round((cgArray.length / 15) * 100);
console.log(`Strand: ${specimen.specimenNum} Percentage C/G: ${percentageCG}`);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment