Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created September 13, 2022 21:01
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/2bf1bf9b127b0953a122573f86d6a525 to your computer and use it in GitHub Desktop.
Save codecademydev/2bf1bf9b127b0953a122573f86d6a525 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 strand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand = []
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}
function pAequorFactory(num, arr) {
return {
specimenNum: num,
dna: arr,
mutate() {
let ind = Math.floor(Math.random() * 15);
let base = this.dna[ind];
let newBase;
do {
newBase = returnRandBase();
this.dna.splice(ind, 1, newBase);
}
while (base === newBase);
return this.dna;
},
compareDNA(obj) {
let count = 0;
for (i = 0; i < this.dna.length; i++) {
if (this.dna[i] === obj.dna[i]) {
count++;
};
};
const common = ((count / this.dna.length) * 100).toFixed(1);
console.log('Specimen 1 and Specimen 2 have ' + common + '% DNA in common.')
},
willLikelySurvive() {
let cg = 0;
for (i = 0; i < this.dna.length; i++) {
if (this.dna[i] === 'C' || this.dna[i] === 'G') {
cg++;
};
};
const survive = ((cg / this.dna.length) * 100);
if (survive >= 60) {
return true;
} else {
return false;
}
}
}
}
const survivors = [];
function create30() {
let x = 1;
while (survivors.length < 30) {
const pAequor = pAequorFactory(x, mockUpStrand());
if (pAequor.willLikelySurvive() === true) {
survivors.push(pAequor);
x++;
};
};
return survivors;
}
create30();
console.log(survivors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment