Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created January 8, 2021 19:04
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/8c2d78d84285f3dcc646a51f7c8c0fe2 to your computer and use it in GitHub Desktop.
Save codecademydev/8c2d78d84285f3dcc646a51f7c8c0fe2 to your computer and use it in GitHub Desktop.
Codecademy export
//Follow me on GitHub: https://github.com/khoiuna
// 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, array) => {
let obj = {
specimenNum: num,
dna: array,
//
mutate() {
while(this.dna[Math.floor(Math.random() * this.dna.length)] === this.dna[Math.floor(Math.random() * this.dna.length)]) {
this.dna[Math.floor(Math.random() * this.dna.length)] = returnRandBase();
}
return this.dna;
},
//
compareDNA(pAequor) {
let count = 0;
for(let i in this.dna) {
if(this.dna[i] === pAequor[i]) count++;
}
console.log(`specimen #1 and specimen #2 have ${count / this.dna.length * 100} DNA in common`);
},
//
willLikelySurvive() {
let count = 0;
this.dna.forEach(el => {
if(el === 'C' || el === 'G') count++;
})
return count / this.dna.length >= .6;
}
};
//
return obj;
};
let pAequorArr = [];
for(let i = 1; i < 31; i++) {
if(!pAequorFactory(i, mockUpStrand()).willLikelySurvive()) continue;
pAequorArr.push(pAequorFactory(i, mockUpStrand()))
}
console.log(pAequorArr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment