Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 10, 2020 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codecademydev/5a90fd9d6529a1adb927f7b743a6cd23 to your computer and use it in GitHub Desktop.
Save codecademydev/5a90fd9d6529a1adb927f7b743a6cd23 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 = (specimenNum, dna) => {
return {
specimenNum,
dna,
mutate(){
const randIndex = Math.floor(Math.random()*this.dna.length);
let newBase = returnRandBase();
while (this.dna[randIndex]===newBase){
newBase=returnRandBase();
}
this.dna[randIndex] = newBase;
return this.dna;
},
compareDNA(otherOrg) {
const similarities = this.dna.reduce((acc, curr, idx, arr) => {
if (arr[idx] === otherOrg.dna[idx]) {
return acc + 1;
} else {
return acc;
}
}, 0);
const percentOfDNAEqual= (common/this.dna.length)*100;
const percentTo2Deci = percentOfDNAEqual.toFixed(2);
console.log(`${this.specimenNum} and ${PAequor.specimenNum} have ${percentageTo2Deci}% DNA in common.`);
},
willLikelySurvive () {
const chances = this.dna.filter (el => el === 'C' || el === "G");
return chances.length/this.dna.length >=0.6;
} ,
}
};
const survivingSpecimen = [];
let idCounter = 1;
while (survivingSpecimen.length < 30){
let newOrg = pAequorFactory(idCounter, mockUpStrand());
if (newOrg.willLikelySurvive()){
survivingSpecimen.push(newOrg);
}
idCounter++;
}
console.log(survivingSpecimen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment