-
-
Save codecademydev/1e309a0efa407b7a318c5f2c73fe7cc4 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
function pAequorFactory(specimenNum,dna){ | |
return { | |
specimenNum, | |
dna, | |
mutate(){ | |
const index = Math.floor(Math.random()*15); | |
let next = returnRandBase(); | |
while(this.dna[index] == next){ | |
next = returnRandBase(); | |
} | |
this.dna[index] = next; | |
return this.dna; | |
}, | |
compareDNA(pAequor){ | |
let sameCount = 0; | |
this.dna.forEach((v,i)=>{ | |
if(v == pAequor.dna[i]) ++sameCount; | |
}); | |
const rate = sameCount / this.dna.length * 100; | |
console.log(`specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${rate}% DNA in common`); | |
}, | |
willLikelySurvive(){ | |
const surviveLen = this.dna.filter(i=>/[C,G]/.test(i)).length; | |
return surviveLen / this.dna.length >= 0.6; | |
}, | |
complementStrand(){ | |
return this.dna.map(i=>{ | |
if(i == 'A') return 'T'; | |
if(i == 'T') return 'A'; | |
if(i == 'C') return 'G'; | |
if(i == 'G') return 'C'; | |
}); | |
}, | |
}; | |
} | |
// const a=pAequorFactory(1,mockUpStrand()); | |
// const b=pAequorFactory(2,mockUpStrand()); | |
// console.log(a.dna); | |
// console.log(b.dna); | |
// a.compareDNA(b); | |
// console.log(a.willLikelySurvive()); | |
const pAequors = []; | |
let p; | |
while(pAequors.length < 30){ | |
p = pAequorFactory(pAequors.length + 1,mockUpStrand()); | |
if(p.willLikelySurvive()){ | |
pAequors.push(p); | |
} | |
} | |
// console.log(pAequors); | |
const c=pAequorFactory(2,mockUpStrand()); | |
// console.log(c.dna); | |
// console.log(c.complementStrand()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment