Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 6, 2020 12: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/610072b73cb4d0a16d9a9c2b96996dcb to your computer and use it in GitHub Desktop.
Save codecademydev/610072b73cb4d0a16d9a9c2b96996dcb 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 = (num, arr) => {
return {
specimenNum: num,
dna: arr,
mutate() {
randomNo = Math.floor(Math.random()*15);
let mutatingBase = this.dna[randomNo];
let newBase;
do {
newBase = returnRandBase();
} while(newBase === mutatingBase)
if (newBase !== mutatingBase) {
this.dna.splice(randomNo, 1, newBase);
}
return this.dna
},
compareDNA(pAequor) {
let otherDNA = pAequor.dna;
let thisDNA = this.dna;
let count = 0;
let length = thisDNA.length
for(let i=0; i<thisDNA.length; i++) {
if(thisDNA[i] === otherDNA[i]) {
count = count + 1;
};
}
console.log(count);
console.log(length);
const similarity = (count/length)*100
console.log(`The similarity is: ${similarity}%`);
},
willLikelySurvive() {
let count = 0;
let length = this.dna.length;
for (let i=0; i<this.dna.length; i++) {
if(this.dna[i] === 'C' || this.dna[i] === 'G') {
count++
}
}
let percentage = (count/length)*100;
if (percentage >= 60) {
return true;
} else {
return false;
}
},
}
};
let thirtySurvivors = [];
let number = 0;
const create30Survivors = () => {
do {
number++;
let newOrganism = pAequorFactory(number, mockUpStrand());
if (newOrganism.willLikelySurvive) {
thirtySurvivors.push(newOrganism)
}
} while (thirtySurvivors.length < 30)
};
create30Survivors();
console.log(thirtySurvivors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment